繁体   English   中英

Android打开下载的文件

[英]Android open downloaded file

我有一个关于下载pdf文件并使用手机上安装的pdf阅读器应用程序打开它的问题。 我是初学者并且正在努力工作,但却陷入了困境。

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

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

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

这是我在经过漫长的一天搜索后如何做到的,您的代码帮我解决了一下:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM