Reputation: 331
I have a few pictures in my app, and I want to open them in the default viewer when the user taps on them. Here's my code:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory().toString() + "/Pictures/mypic.jpg");
intent.setData(uri);
intent.setType("image/jpeg");
startActivity(intent);
The default viewer is indeed opened, but it shows its "home screen", showing all the folders and galleries. It doesn't display the selected picture.
What am I doing wrong?
Upvotes: 0
Views: 76
Reputation: 11214
With following code placed in a try block you can open any file: Add the catch blocks your self.
String FileName = ...full path of file...
MimeTypeMap map = MimeTypeMap.getSingleton();
String extension = map.getFileExtensionFromUrl(FileName.toLowerCase().replace(" ", "")); // does not work with spaces in filename
String mimetype = map.getMimeTypeFromExtension(extension);
Toast.makeText(context, FileName + "\nMimeType: " + mimetype + "\nExtension: " + extension , Toast.LENGTH_SHORT).show();
if ( mimetype == null )
{
mimetype = "text/plain";
Toast.makeText(context, "MimeType: " + mimetype, Toast.LENGTH_SHORT).show();
}
String Url = "file://" + FileName;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(Url), mimetype);
context.startActivity(intent);
Upvotes: 1