安卓:如何在内置相册中显示图片

4
我正在尝试在内置图库中打开一张图片,但是它没有显示出来。我记录了URI,如下所示:
06-08 15:53:02.232: D/URI(3231): file:///mnt/sdcard/com.jesper.indkoeb/nm-image.jpg
这是正确的位置。
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();  
                    intent.setAction(android.content.Intent.ACTION_VIEW);  
                    File file = new File(uri.get(position));
                    Log.d("URI",uri.get(position));
                    intent.setDataAndType(Uri.fromFile(file), "image/*");  
                    context.startActivity(intent);

                }

            });
1个回答

9
以下代码片段将会帮助您:

以下代码片段将会帮助您。

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///mnt/sdcard/image/Apples.jpg"),"image/*");
startActivity(intent);

@VipulShah 我试了你的代码,但它需要大约7-8秒才能在相册中显示,我正在使用Android 2.3。 - umesh
如果分别调用setData()和setType(),会抛出“未找到活动”异常(就像找不到任何支持的应用程序一样)。 - samus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接