【问题标题】:Android - Obtain Uri to image on sdcardAndroid - 获取 Uri 以在 sdcard 上成像
【发布时间】:2011-07-07 23:42:39
【问题描述】:

我在图库中有 .jpg 的绝对路径。 如何获取此图像的“内容://” Uri,以便我可以像这样使用此 Uri:

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

我需要启动此意图,以便查看图像。 当我只有图像的绝对路径时,如何获取 uri?

【问题讨论】:

    标签: android android-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() 方法从文件中检索 URI。

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

    【讨论】:

    • 谢谢,但它不能正常工作。首先是报错,因为Intent()构造函数的第二个参数应该是android.net.Uri,而且是java.net.URI。另外,方法 yourFile.toUri() 返回 file:/ URI,而我想要 content:// URI。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多