2011-07-07 34 views
0

我有绝对路径在画廊中的.jpg。 我怎样才能获得一个“内容://” URI这个形象,这样我就可以使用这个URI是这样的:Android - 获取Uri在SD卡上的图像

Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 

我需要启动这个意图,所以我可以查看图像。 当我只有图像的绝对路径时,如何获得uri?

回答

1

首先,您需要从SDCard中读取文件。

参见:

reading a specific file from sdcard in android

http://developer.android.com/reference/java/io/File.html

File dir = Environment.getExternalStorageDirectory(); 
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext"); 

然后调用File.toUri()方法来从文件retreive的URI。

Intent intent = new Intent(Intent.ACTION_VIEW, yourFile.toURI()); 
startActivity(intent); 
+1

谢谢你,但它不是正确的。首先,出现错误,因为Intent()构造函数的第二个参数应该是android.net.Uri,它是java.net.URI。另外,方法yourFile.toUri()返回文件:/ URI,而我想要content:// URI。 – Gabriel