android - 想要在 Android 中的适当应用程序中打开下载的文件

标签 android android-dialog file-format android-download-manager

我必须从我的服务器下载文档到我的应用程序并保存到私有(private)文件夹中。

文档可以是 .doc、.pdf、.docs、.jpg 等任何内容。

现在,我想要的是,下载完成后,我显示一个警报对话框来打开文档。

当我单击笔按钮时,然后在适当的应用程序中打开该特定文件,如果该应用程序不可用,则显示对话框以下载应用程序。

请给我一些提示。

提前致谢。

最佳答案

试试这个方法:

邮件附件打开

        File file = new File(yourdownloadfilePATH);

        if ((download_file_name.endsWith(".pdf")) || (download_file_name.endsWith(".PDF"))){            

            if(file.exists() && file.length()!=0){

            Intent intent = new Intent();

            intent.setClassName("com.adobe.reader","com.adobe.reader.AdobeReader");

            intent.setAction(android.content.Intent.ACTION_VIEW);

            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            Uri uri = Uri.fromFile(file);

            intent.setDataAndType(uri, "application/pdf");

            try {

                startActivity(intent);

            } catch (Exception e) {

                                AlertDialog.Builder builder = new AlertDialog.Builder(youractivity.this);
                                builder.setTitle("No Application Found");
                                    builder.setMessage("Download application from Android Market?");
                                    builder.setPositiveButton(
                                            "Yes, Please",
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    Intent marketIntent = new Intent(
                                                            Intent.ACTION_VIEW);
                                                    marketIntent.setData(Uri
                                                            .parse("market://details?id=com.adobe.reader"));
                                                    startActivity(marketIntent);
                                                }
                                            });
                                    builder.setNegativeButton("No, Thanks",
                                            null);
                                    builder.create().show();
            }

    }
 }  else if ((download_file_name.endsWith(".jpg"))||  (download_file_name.endsWith(".bmp"))||
(download_file_name.endsWith(".BMP"))||(download_file_name.endsWith(".png"))||  (download_file_name.endsWith(".PNG"))||(download_file_name.endsWith(".gif"))||   (download_file_name.endsWith(".GIF"))||
                            (download_file_name.endsWith(".JPG"))) {

        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "image/*");       
        startActivity(intent);

    }
else if ((download_file_name.endsWith(".txt"))) {


    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "text/html");
    startActivity(intent);

}

关于android - 想要在 Android 中的适当应用程序中打开下载的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22556029/

相关文章:

android - 关于Local notification的几个问题

java - Home.ImagePagerAdapter 类型未定义方法 setOnPageChangeListener(ViewPager.SimpleOnPageChangeListener)

java - 带有游戏分数的自定义对话框 android

android - 如何在 Android 应用程序中将任何格式(.txt、.Doc)文件转换为 epub 文件

android - 无法在 Android studio 中安装 TFS 插件

Android Listview 搜索到特定位置

安卓工作室 “Attempt to invoke virtual method on a null object reference”

android - 如何在 Android 上居中自定义对话框标题

python - python列表列表的用户可读文件格式

hex - 为什么PE文件中的MZ DOS Header Signature是0x54AD?