Reputation: 665
I am trying to open files using intent action but i can't for pdf and image files
For image all application going to crash (including gallery app)
For doc/docx i am using office suite but gives runtime exception from package (java.lang.RuntimeException). Please see code below :
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
if (extension.equalsIgnoreCase("jpg")
| extension.equalsIgnoreCase("jpeg")
| extension.equalsIgnoreCase("png")
| extension.equalsIgnoreCase("bitmap")) {
intent.setDataAndType(Uri.parse(fileName), "image/*");
} else if (extension.equalsIgnoreCase("xml")
| extension.equalsIgnoreCase("txt")
| extension.equalsIgnoreCase("csv")) {
intent.setDataAndType(Uri.parse(fileName), "text/*");
} else if (extension.equalsIgnoreCase("mp4")
| extension.equalsIgnoreCase("3gp")) {
intent.setDataAndType(Uri.parse(fileName), "video/*");
} else if (extension.equalsIgnoreCase("pdf")) {
intent.setDataAndType(Uri.parse(fileName), "application/pdf");
System.out.println("Pdf file to open "+fileName);
} else if (extension.equalsIgnoreCase("doc")
| extension.equalsIgnoreCase("docx")) {
intent.setDataAndType(Uri.parse(fileName), "application/word");
}
context.startActivity(intent);
But if i tried to open those file by going file explorer both file opening correctly.
Upvotes: 0
Views: 1188
Reputation: 11
Try this:
Intent intent = new Intent(Intent.ACTION_VIEW);
//intent.setAction(Intent.ACTION_VIEW);
if (extension.equalsIgnoreCase("jpg")
| extension.equalsIgnoreCase("jpeg")
| extension.equalsIgnoreCase("png")
| extension.equalsIgnoreCase("bitmap")) {
intent.setDataAndType(Uri.parse("file://"+fileName), "image/*");
} else if (extension.equalsIgnoreCase("xml")
| extension.equalsIgnoreCase("txt")
| extension.equalsIgnoreCase("csv")) {
intent.setDataAndType(Uri.parse("file://"+fileName), "text/*");
} else if (extension.equalsIgnoreCase("mp4")
| extension.equalsIgnoreCase("3gp")) {
intent.setDataAndType(Uri.parse("file://"+fileName), "video/*");
}else if(extension.equalsIgnoreCase("pdf")){
intent.setDataAndType(Uri.parse("file://"+fileName), "application/pdf");
}else if( extension.equalsIgnoreCase("doc")
| extension.equalsIgnoreCase("docx")){
intent.setDataAndType(Uri.parse("file://"+fileName), "text/*");
}
// else
// if(extension.equalsIgnoreCase("mp3")|extension.equalsIgnoreCase("amr")|extension.equalsIgnoreCase("wav")){
// intent.setDataAndType(Uri.parse(fileName), "audio/mp3");
// }
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
Upvotes: 1
Reputation: 665
I think its issue of the file uri class not returning file I found the error
It was Uri.parse(fileName)
I used now Uri.fromFile(fileName) its working now.
Upvotes: 0