繁体   English   中英

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

[英]Want to open downloaded file in appropriate application in Android

我必须将文件从服务器下载到应用程序中,然后保存到私有文件夹中。

该文档可以是.doc, .pdf, .docs, .jpg等。

现在,我想要的是,下载完成后,我将显示一个“ Alert Dialog以打开文档。

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

给我一些提示。

提前致谢。

尝试这种方式:

邮件附件打开

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

}

不确定,但是我认为这会对您有所帮助:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(your_file);
String extension = file .split("\\.");

//For text extensions like text or any textfiles
if(extension == txt){
 intent.setDataAndType(Uri.fromFile(file), "test/*");
}

//For audio extensions like  mp3 or any audio files
else if(extension == mp3 ){ 
 intent.setDataAndType(Uri.fromFile(file), "audio/*");
}

//For video extensions like mp4 or any video files
else if(extension == mp4){
 intent.setDataAndType(Uri.fromFile(file), "video/*");
}
startActivity(intent); 

暂无
暂无

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

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