问题
I have to download a document from my server, to my app and save into a private folder.
The document can be anything like .doc, .pdf, .docs, .jpg
etc.
Now, what I want is, after the download is completed, I show an Alert Dialog
to open the document.
When I click on the pen button then open that particular file in an appropriate application and if the app is not available, then show Dialog
to download app.
Give me some hint on this.
Thanks in advance.
回答1:
Try this way:
Mails attachment open
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);
}
回答2:
Not sure, but I think this will help you a little:
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);
来源:https://stackoverflow.com/questions/22556029/want-to-open-downloaded-file-in-appropriate-application-in-android