how to get Real Path from Uri?

zied picture zied · Apr 29, 2014 · Viewed 8.6k times · Source

I would like to upload file to server. So I use a file chooser. But I have a problem with some application to get the real path from uri. the case if the content is a v ideo, audio or image or if I can get the path directly i have no problem. the methode imlemented are :

public String getRealAudioPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Audio.Media.DATA };

        //This method was deprecated in API level 11
        //Cursor cursor = managedQuery(contentUri, proj, null, null, null);

        CursorLoader cursorLoader = new CursorLoader(
                this,
                contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        int column_index =
                cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

and it is the some for image and video. but my problem is when content is in this forme:

content://com.dataviz.dxtg.documentprovider/document/file%3A%2F%2F%2Fmnt%2Fsdcard%2FDownload%2Arbres.png

I tried the method

private String getFilePathFromContentUri(Uri selectedVideoUri,
                                             ContentResolver contentResolver) {
        String filePath;
        String[] filePathColumn = {MediaStore.MediaColumns.DATA};

        Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    }

So how to get the real path from URi and if there is an other content type .

to get my real path is given below:

//Uri return from external activity
            orgUri = data.getData();
            text1.setText("fichier à envoyer " + orgUri.toString() + "\n");
            // imagepath = orgUri.toString() ;
            //path converted from Uri
            //convertedPath = orgUri.getPath();
            if(orgUri.toString().contains(IMAGEMEDIA)){
                convertedPath = getRealPathFromURI(orgUri);
            } else if(orgUri.toString().contains(AUDIOMEDIA)){
                  convertedPath = getRealAudioPathFromURI(orgUri);

            }
            else if(orgUri.toString().contains(VIDEOMEDIA)){
                convertedPath = getRealvideoPathFromURI(orgUri);

            }else {

                if(orgUri.toString().contains("content")){
                    convertedPath = getFilePathFromContentUri(orgUri,
                            getApplicationContext().getContentResolver());

                }   else {

                convertedPath = orgUri.getPath();
                }
            }

Answer

Imtiyaz Khalani picture Imtiyaz Khalani · Apr 29, 2014

try this one

Uri selectedFile = data.getData();
String path = selectedFile.getPath();

it works for me. if it is not than let me inform i have another way too