在原生Android图库应用中打开图像

时间:2012-04-13 15:11:52

标签: android file android-intent uri

我有一个将图像(jpeg)保存到SD卡上的应用程序。我可以通过手动启动图库来查看该文件。但是我想以编程方式启动图库并在保存文件时查看文件。 --i有文件的绝对路径。

    Intent intent = new Intent ();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setData(uri);
    sendBroadcast (intent);

它不会导致任何错误,但根本没有任何反应。

2 个答案:

答案 0 :(得分:0)

正确的语法是:

startActivity(intent);

startBroadcast(intent);

所以你的代码应该是这样的:

Intent intent = new Intent ();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
startActivity(intent)

此外,您可以考虑执行以下操作:

intent.setDataAndType(uri, "image/*");

而不是

intent.setData(uri);

答案 1 :(得分:0)

尝试此代码

Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

ref-http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/

相关问题