【问题标题】:Android - How to translate image path to content uriAndroid - 如何将图像路径转换为内容 uri
【发布时间】:2013-07-31 09:21:59
【问题描述】:

嘿,我想将图像路径翻译为内容 uri,翻译后出现错误, 我这样称呼这个方法:

uri = getUriFromPath(picturePath);  
tv2.setText(uri.toString());

什么是真的,我称之为这样的方法

查看我的代码,谢谢


这是我将路径转换为 ​​uri 的代码

private Uri getUriFromPath(String filePath) {
long photoId;
Uri photoUri = MediaStore.Images.Media.getContentUri("external");

String[] projection = {MediaStore.Images.ImageColumns._ID};
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = getContentResolver().query(photoUri, projection, MediaStore.Images.ImageColumns.DATA + " LIKE ?", new String[] { filePath }, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(projection[0]);
photoId = cursor.getLong(columnIndex);

cursor.close();
return Uri.parse(photoUri.toString() + "/" + photoId);
}

我得到错误

07-31 08:46:12.239: E/AndroidRuntime(29041): FATAL EXCEPTION: main
07-31 08:46:12.239: E/AndroidRuntime(29041): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dpapayas.mixmatch1/com.dpapayas.mixmatch1.editPhoto}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.os.Looper.loop(Looper.java:213)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.main(ActivityThread.java:4787)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at java.lang.reflect.Method.invoke(Method.java:511)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at dalvik.system.NativeStart.main(Native Method)
07-31 08:46:12.239: E/AndroidRuntime(29041): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.database.CursorWrapper.getLong(CursorWrapper.java:106)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at com.dpapayas.mixmatch1.editPhoto.getUriFromPath(editPhoto.java:75)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at com.dpapayas.mixmatch1.editPhoto.onCreate(editPhoto.java:44)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.Activity.performCreate(Activity.java:5008)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
07-31 08:46:12.239: E/AndroidRuntime(29041):    ... 11 more

【问题讨论】:

  • 您的查询可能没有返回任何内容。所以 indexOutOfBound 异常。
  • 你能帮我解决这个问题吗?
  • 首先检查:MediaStore.Images.ImageColumns._ID的字符串值是什么。它是现有列的有效名称吗?通过变量,它看起来像 ID。该函数需要列名。
  • ohhh,我错了,我将图像保存在 /storage/sdcard0/pictures/mixmatch/"mnm_"+ timeStamp + ".jpg" 那么代码如何正确? :(

标签: android path uri


【解决方案1】:

我就是这样做的。

public static Uri getContentUri(Context context, File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Audio.Media._ID },
            MediaStore.Audio.Media.DATA + "=? ",
            new String[] { filePath }, null);

    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID));
        Uri baseUri = Uri.parse("content://media/external/audio/media");
        return Uri.withAppendedPath(baseUri, "" + id);

    }
}

这里我的参数是文件,并从中查找路径。您可以通过路径本身。

【讨论】:

  • 你应该总是调用 cursor.close() !此外,您的 getColumnIndex 将始终返回 0,因为您在查询中指定了 'projection' 参数,因此您可以将 getColumnIndex 替换为 0。
  • 耶!这是最后的提示。我坐了很长时间才能让它工作。但最后,你成功了 :) 非常感谢!
【解决方案2】:

使用这个

     Uri myUri = Uri.parse("http://www.google.com");

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 1970-01-01
    • 2014-08-13
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    相关资源
    最近更新 更多