dimanche 17 mai 2015

Error when getting RealPathFromURI for Video

I am using this intent to get a video from gallery

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select video"), SELECT_VIDEO);

The in the onActivityResult method, I have

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                String filepath = selectedImageUri.getPath();
                Log.e("Filepath = ",""+filepath);
                String realPath = getRealPathFromURI(selectedImageUri);
                Log.e("Realpath",""+realPath);
               }
            }
         } 

The getRealPathFromURI method is like this

private String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Video.Media.DATA };
        CursorLoader loader = new CursorLoader(MainActivity.this, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        cursor.close();
        return cursor.getString(column_index);
    }

I get the following error after I select a video from the intent

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/video:2232 flg=0x1 }} to activity {com.artqueen.mixer/com.artqueen.mixer.MainActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
            at android.app.ActivityThread.deliverResults(ActivityThread.java:4197)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:4240)
            at android.app.ActivityThread.access$1400(ActivityThread.java:182)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1524)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6141)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
            at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:64)
            at android.database.BulkCursorToCursorAdaptor.getCount(BulkCursorToCursorAdaptor.java:70)
            at android.database.AbstractCursor.checkPosition(AbstractCursor.java:425)
            at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
            at android.database.CursorWrapper.getString(CursorWrapper.java:114)
            at com.artqueen.mixer.MainActivity.getRealPathFromURI(MainActivity.java:87)
            at com.artqueen.mixer.MainActivity.onActivityResult(MainActivity.java:58)
            at android.app.Activity.dispatchActivityResult(Activity.java:6632)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:4193)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:4240)
            at android.app.ActivityThread.access$1400(ActivityThread.java:182)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1524)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6141)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Please help me. Why this error is showing up? Is there any other way to get RealPath from URI so that I could play the video in a VideoView. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire