2

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();
                }
            }
zied
  • 201
  • 3
  • 7
  • 17

3 Answers3

2

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

Imtiyaz Khalani
  • 2,037
  • 18
  • 32
2

This works for me...

    public static String getPath(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >=  
     Build.VERSION_CODES.KITKAT;
    Log.i("URI",uri+"");
    String result = uri+"";
    // DocumentProvider 
    //  if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 
    if (isKitKat && (result.contains("media.documents"))) {

        String[] ary = result.split("/");
        int length = ary.length;
        String imgary = ary[length-1];
        final String[] dat = imgary.split("%3A");

        final String docId = dat[1];
        final String type = dat[0];

        Uri contentUri = null;
        if ("image".equals(type)) {
            contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        } else if ("video".equals(type)) {

        } else if ("audio".equals(type)) {
        } 

        final String selection = "_id=?";
        final String[] selectionArgs = new String[] {
                dat[1]
        }; 

        return getDataColumn(context, contentUri, selection, selectionArgs);
    } 
    else 
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        return getDataColumn(context, uri, null, null);
    } 
    // File 
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    } 

    return null; 
} 


    public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {

        Cursor cursor = null;
    final String column = "_data";
    final String[] projection = {
            column
    }; 

    try { 
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                null); 
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        } 
    } finally { 
        if (cursor != null)
            cursor.close();
    } 
    return null; 
} 
Irshad
  • 3,071
  • 5
  • 30
  • 51
parvez rafi
  • 464
  • 4
  • 20
0

It is a problem of decode. So the Solution is to add decode to the method. as given below:

private String getFilePathFromContentUri(Uri selectedVideoUri,
                                             ContentResolver contentResolver) throws UnsupportedEncodingException {
        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();
        String result = java.net.URLDecoder.decode(filePath, "UTF-8");
        return result;
    }

my onActivityResult:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK){

            //image.setImageBitmap(null);

            //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")){
                    try {
                        convertedPath = getFilePathFromContentUri(orgUri,
                                getApplicationContext().getContentResolver());
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }

                }   else {

                convertedPath = orgUri.getPath();
                }
            }

           // text2.setText("Real Path: " + convertedPath + "\n");
            imagepath =  convertedPath;
 uriFromPath = Uri.fromFile(new File(convertedPath));

}
}
zied
  • 201
  • 3
  • 7
  • 17