开发者

Opening a recently written file using Intents

开发者 https://www.devze.com 2023-01-27 19:48 出处:网络
We are downloading a file from a server a开发者_高级运维nd 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 f

We are downloading a file from a server a开发者_高级运维nd 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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号