如何在android中使用默认可用的应用程序在android中打开下载的文件

如何在android中使用默认可用的应用程序在android中打开下载的文件,android,file,download,Android,File,Download,我正在从服务器下载一个文件。下载完成后,我必须打开一个文件。知道问题是文件可能是任何类型的,所以我不能指定和意图调用以打开静态名称的文件,就像我们打开PDF文件一样。 我想要的是,当一个文件被下载时,它会搜索是否有任何应用程序可以打开该文件,否则它会弹出窗口。 我正在做所有这些内部碎片。 以下是我的下载代码: public class DownloadFile extends AsyncTask<String, Void, Integer> { String file_nam

我正在从服务器下载一个文件。下载完成后,我必须打开一个文件。知道问题是文件可能是任何类型的,所以我不能指定和意图调用以打开静态名称的文件,就像我们打开PDF文件一样。 我想要的是,当一个文件被下载时,它会搜索是否有任何应用程序可以打开该文件,否则它会弹出窗口。 我正在做所有这些内部碎片。 以下是我的下载代码:

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;
        }
    }
}
公共类下载文件扩展异步任务{
字符串文件_name=“”;
文件sdcard=Environment.getExternalStorageDirectory();
@凌驾
受保护的整数doInBackground(字符串…参数){
//TODO自动生成的方法存根
试一试{
HttpURLConnection url_conn=null;
字节[]bffr;
长totalSize=0;
文件目录=新文件(
Environment.getExternalStorageDirectory()
+“/xyz/download”);
mkdirs()目录;
//06-03 17:57:41.160:D/文件名(6882):
文件名=”;
文件名=参数[0];
Log.d(“文件名”,file_name.toString());
url_conn=(HttpURLConnection)(新url(“http://example.com/uploads/“+文件名”).openConnection();
url_conn.setRequestMethod(“GET”);
url_conn.setDoOutput(真);
url_conn.connect();
如果(url\u conn.getContentLength()>0){
File imgFile=新文件(sdcard+“/xyz/download/”,文件名);
FileOutputStream fos=新的FileOutputStream(imgFile);
InputStream is=url_conn.getInputStream();
totalSize=url_conn.getContentLength();
//Log.d(“文件下载大小”,totalSize+”);
长总计=0;
bffr=新字节[1024];
int bufferLength=0;
而((bufferLength=is.read(bffr))>0){
总长度+=缓冲区长度;
出版进度(“+(int)((总计*100)/totalSize));
fos.write(bffr,0,缓冲区长度);
}
fos.close();
}否则
Log.w(文件名.toString(),“未找到文件”);
返回0;
}捕获(例外e){
e、 printStackTrace();
返回-1;
}
}
私有void publishProgress(字符串…进程){
//TODO自动生成的方法存根
mprogressDialog.setProgress(Integer.parseInt(进程[0]);
}
受保护的void onPostExecute(未使用的整数){
Log.d(“下载文件后”、“下载文件”);
开关(未使用){
案例0:
mprogressDialog.disclose();
安装意图=新意图(意图.动作\u视图);
install.setDataAndType(Uri.fromFile(新文件(sdcard+“/xyz/download/”,文件名)),
“MIME-TYPE”);
install.setFlags(Intent.FLAG\u ACTIVITY\u NEW\u TASK);
app.getBaseContext().startActivity(安装);
打破
}
}
}
在post execute中,我尝试使用Intent打开它,但没有成功。 任何想法都值得赞赏

 install.setDataAndType(Uri.fromFile(new File(sdcard + "/xyz/download/",file_name)),
                    "MIME-TYPE");
您必须根据文件类型设置MIME-TYPE,它将在设备中的可用应用程序中打开它
参考此

我想您必须传递有效的mime类型才能获得应用程序选择器弹出窗口。 您可以从文件名或文件实例获取mimeType

String fileName = "/path/to/file";
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();

// only by file name
String mimeType = mimeTypesMap.getContentType(fileName);
来源下载后执行此操作--


如果您的targetSdkVersion>=24,则我们必须使用FileProvider类授予对特定文件或文件夹的访问权限,以使其他应用程序可以访问这些文件或文件夹。
String fileName = "/path/to/file";
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();

// only by file name
String mimeType = mimeTypesMap.getContentType(fileName);
 MimeTypeMap myMime = MimeTypeMap.getSingleton();
    Intent newIntent = new Intent(Intent.ACTION_VIEW);
    String mimeType = myMime.getMimeTypeFromExtension(fileExt(path).substring(1));
    newIntent.setDataAndType(Uri.fromFile(new File(path)), mimeType);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        ReadMailActivity.this.startActivity(newIntent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(ReadMailActivity.this, "No handler for this type of file.", Toast.LENGTH_LONG).show();
    }