【问题标题】:How to open downloaded file in android with default availabe application in android如何使用android中的默认availabe应用程序在android中打开下载的文件
【发布时间】:2015-07-13 07:12:49
【问题描述】:

我在下载完成后从服务器下载一个文件,我必须打开一个文件。知道问题是文件可以是任何类型,所以我不能像打开 PDF 文件那样指定和 Intent 调用来打开具有静态名称的文件。 我想要的是当一个文件被下载时,它会搜索是否有任何应用程序可以打开文件,否则它会弹出。 我在片段中做这一切。 这是我的下载代码:

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;
        }
    }
}

在执行后我尝试使用 Intent 打开它,但没有奏效。 任何想法表示赞赏

【问题讨论】:

    标签: android file download


    【解决方案1】:
        File file = new File(filePath);
        MimeTypeMap map = MimeTypeMap.getSingleton();
        String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
        String type = map.getMimeTypeFromExtension(ext);
    
        if (type == null)
            type = "*/*";
    
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data = Uri.fromFile(file);
    
        intent.setDataAndType(data, type);
    
        startActivity(intent);
    

    【讨论】:

    • 如果您的 targetSdkVersion >= 24,那么我们必须使用 FileProvider 类来授予对特定文件或文件夹的访问权限,以便其他应用程序可以访问它们。
    【解决方案2】:
     install.setDataAndType(Uri.fromFile(new File(sdcard + "/xyz/download/",file_name)),
                        "MIME-TYPE");
    

    您必须根据文件类型设置 MIME-TYPE,它将在设备的可用应用程序中打开它 参考这个https://stackoverflow.com/a/24134677/3303075

    【讨论】:

      【解决方案3】:

      我猜你必须传递有效的 mime-type 才能获得应用选择器弹出窗口。 您可以从文件名或文件实例中获取 mimeType。

      String fileName = "/path/to/file";
      MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
      
      // only by file name
      String mimeType = mimeTypesMap.getContentType(fileName);
      

      来源https://stackoverflow.com/a/1902146/942224

      【讨论】:

        【解决方案4】:

        下载后执行此操作--

         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();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-17
          • 1970-01-01
          相关资源
          最近更新 更多