Java Android 打开下载的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21335129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:07:33  来源:igfitidea点击:

Android open downloaded file

javaandroidpdfandroid-download-manager

提问by Msmit1993

I have a question about downloading a pdf file and opening it with an pdf reader application installed on the phone. I'm new to android and working my way up but got stuck on this part.

我有一个关于下载 pdf 文件并使用手机上安装的 pdf 阅读器应用程序打开它的问题。我是 android 的新手,正在努力工作,但在这部分卡住了。

So what i have now: I have an activity that for now starts downloading a pdf file and tries to open is with an intent. For now everything is static so thats why i have a set url.

所以我现在所拥有的:我有一个活动,现在开始下载 pdf 文件并尝试打开是有意图的。现在一切都是静态的,所以这就是为什么我有一个设置的 url。

private void DownloadFile(){
        DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
        Uri Download_Uri = Uri.parse("http://awebiste.adomain/afile.pdf");
        DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);    
        request.setAllowedOverRoaming(false);   
        request.setTitle("My Data Download");
        request.setDescription("Android Data download using DownloadManager.");
        request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,"test.pdf");

        Long downloadReference = downloadManager.enqueue(request);

        if (downloadReference != null){
            Intent target = new Intent(Intent.ACTION_VIEW);
            target.setDataAndType(Uri.parse(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/test.pdf"), "application/pdf");
            target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            Log.v("OPEN_FILE_PATH", getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/test.pdf");

            startActivity(target);

        } else {
            //TODO something went wrong error
        }
    }

Now the file is downloaded and saved in the application folder under /storage.sdcard0/Android/data/<application>/files/downloadand from the documents browser the file can be opent. But when i use the intent at the button of my code i get a toast that the file can not be opent. After some searching on google i think its a permission problem because these files are private to the application. So how do I make these files public?

现在文件被下载并保存在/storage.sdcard0/Android/data/<application>/files/download文件浏览器下的应用程序文件夹中,文件可以打开。但是当我在我的代码按钮上使用意图时,我得到一个无法打开文件的敬酒。在谷歌上搜索后,我认为这是一个权限问题,因为这些文件是应用程序私有的。那么如何公开这些文件呢?

回答by Muhammad

Here's How I did it after a long day of search,Your code helped me a little to solve it:

经过一整天的搜索,这是我的方法,您的代码帮助我解决了这个问题:

String name;
DownloadManager mManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show_interview);
    Button down = (Button) findViewById(R.id.download);
    down.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            down();

        }
    });

}

public void down() {
    mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
    DownloadManager.Request request = new DownloadManager.Request(
            downloadUri)
            .setAllowedOverRoaming(false)
            .setTitle("Downloading")
            .setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, name + "CV.pdf")
            .setDescription("Download in progress").setMimeType("pdf");
}

@Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    registerReceiver(broadcast, intentFilter);
}

public void showPdf() {
    try {
        File file = new File(Environment.getExternalStorageDirectory()
                + "/Download/" + name + "CV.pdf");//name here is the name of any string you want to pass to the method
        if (!file.isDirectory())
            file.mkdir();
        Intent testIntent = new Intent("com.adobe.reader");
        testIntent.setType("application/pdf");
        testIntent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        testIntent.setDataAndType(uri, "application/pdf");
        startActivity(testIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

BroadcastReceiver broadcast = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        showPdf();
    }
};