Danang Kukuh
Danang Kukuh

Reputation: 57

Android - How to translate image path to content uri

hey i want to translate image path to content uri, and i getting error after i translated, and I call the method like this:

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

and what is true i call method like this

see my code, thanks


this is my code for translate path to 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);
}

and i getting error

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

Upvotes: 2

Views: 2685

Answers (2)

Shivang Trivedi
Shivang Trivedi

Reputation: 2182

use this

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

Upvotes: 0

Nizam
Nizam

Reputation: 5731

This is how I did it.

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);

    }
}

Here my parameter is File, and finding path from it. You can pass the path itself.

Upvotes: 4

Related Questions