我正在从服务器上下载完成下载的文件我必须打开一个文件。知道问题是文件可能是任何类型的,所以我不能指定和Intent调用打开一个静态名称的文件,就像我们打开一个PDF文件一样。 我想要的是当一个文件被下载时它将搜索是否有任何应用程序可用来打开文件,否则它会显示弹出。 我正在做所有这些内部片段。 这里我下载的代码:如何在Android中使用默认的可用应用程序在android中打开下载的文件
public class DownloadFile extends AsyncTask<String, Void, Integer> {
String file_name = "";
File sdcard = Environment.getExternalStorageDirectory();
@Override
protected Integer doInBackground(String... params) {
// TODO Auto-generated method stub
try {
HttpURLConnection url_conn = null;
byte[] bffr;
long totalSize = 0;
File directory = new File(
Environment.getExternalStorageDirectory()
+ "/xyz/download");
directory.mkdirs();
// 06-03 17:57:41.160: D/file name(6882):
file_name = "";
file_name = params[0];
Log.d("file name", file_name.toString());
url_conn = (HttpURLConnection) (new URL("http://example.com/uploads/" + file_name)).openConnection();
url_conn.setRequestMethod("GET");
url_conn.setDoOutput(true);
url_conn.connect();
if (url_conn.getContentLength() > 0) {
File imgFile = new File(sdcard + "/xyz/download/",file_name);
FileOutputStream fos = new FileOutputStream(imgFile);
InputStream is = url_conn.getInputStream();
totalSize = url_conn.getContentLength();
// Log.d("File Download Size ",totalSize+"");
long total = 0;
bffr = new byte[1024];
int bufferLength = 0;
while ((bufferLength = is.read(bffr)) > 0) {
total += bufferLength;
publishProgress("" + (int) ((total * 100)/totalSize));
fos.write(bffr, 0, bufferLength);
}
fos.close();
} else
Log.w(file_name.toString(), "FILE NOT FOUND");
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
private void publishProgress(String... process) {
// TODO Auto-generated method stub
mprogressDialog.setProgress(Integer.parseInt(process[0]));
}
protected void onPostExecute(Integer unused) {
Log.d("after downloading file ", "file downloaded ");
switch (unused) {
case 0:
mprogressDialog.dismiss();
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(sdcard + "/xyz/download/",file_name)),
"MIME-TYPE");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
app.getBaseContext().startActivity(install);
break;
}
}
}
在后执行I曾尝试使用意向来打开它,但没有奏效。 任何想法表示赞赏