Question

We are downloading a file from a server and writing it to folder on sdcard. As soon as the write finishes, we perform a Media Scan on that folder. When the user tries to open the file for viewing, we try to leverage upon the Intent mechanism of Android and raise an Intent as follows:

Intent myIntent = new Intent(Intent.ACTION_VIEW,Uri.fromFile(tempFile));
// MIME_TYPE could be either of: image/*, video/*, audio/*, text/*
myIntent.setType(MIME_TYPE);
startActivity(Intent.createChooser(myIntent, "Open file using..."));

On emulator, the camera Application is launched for this Intent but the application crashes with NPE. And on MOTOROLA Milestone, we are getting a Toast which says: "No image to show". Note that the photo is actually displayed in Media Gallery of both emulator and phone as soon as the scan finishes.

Not sure what has been missed here; please help.

Was it helpful?

Solution

strangely, changing from

Intent myIntent = new Intent(Intent.ACTION_VIEW,Uri.fromFile(tempFile));
// MIME_TYPE could be either of: image/*, video/*, audio/*, text/*
myIntent.setType(MIME_TYPE);

to

Intent myIntent = new Intent(Intent.ACTION_VIEW);
// MIME_TYPE could be either of: image/*, video/*, audio/*, text/*
myIntent.setDataAndType(Uri.fromFile(tempFile), MIME_TYPE);

worked!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top