【问题标题】:Android open downloaded fileAndroid打开下载的文件
【发布时间】:2014-01-24 14:27:33
【问题描述】:

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

所以我现在拥有的: 我有一个活动,现在开始下载 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 下的应用程序文件夹中,并且可以从文档浏览器中打开该文件。但是当我在我的代码按钮上使用意图时,我得到一个文件无法打开的祝酒词。在谷歌上进行一些搜索后,我认为这是一个权限问题,因为这些文件是应用程序私有的。那么如何公开这些文件呢?

【问题讨论】:

标签: java android pdf android-download-manager


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多