0

i am implementing the functionality to save file in my app which is being sent by some exteral application.

i have provided support for single and mulitple files. Provided handling for all kind of files.

But i am not able to handle the following scenario.

I view a file from an email client -> View it in QuickOffice -> Click on send -> Choose my app->Then click on save in my app.

In that i get the path in following wrapped in the exception

java.io.FileNotFoundException: /file:/data/data/com.qo.android.sp.oem/files/temp/Error.log: open failed: ENOENT (No such file or directory)

I have seen this post which is quite useful for handling uri which has content scheme Get filename and path from URI from mediastore

Below is my code

Uri uri = (Uri) iterator.next();
                            if ("content".equals(uri.getScheme())) {
                                filePath = getFilePathFromContentUri(uri, hostAcitvity.getContentResolver());
                            }
                            else {
                                filePath = uri.getPath();
                            }
                            fileName = uri.getLastPathSegment();
                            fileSize = hostAcitvity.getContentResolver().openInputStream(uri).available();

Code for getFilePathFromContentUri

private String getFilePathFromContentUri(Uri selectedVideoUri, ContentResolver contentResolver)
    {
        String filePath;
        String[] filePathColumn = { 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;
    }

Then i wrap the path in a FileInputStream which is throwing the above exception Not able to resolve the file path properly. Is this the correct way of finding the path ?

cheers, Saurav

Community
  • 1
  • 1
saurav
  • 5,388
  • 10
  • 56
  • 101

1 Answers1

0

I have seen this post which is quite useful for handling uri which has content scheme

That never worked reliably and will work even less reliably in the future.

Is this the correct way of finding the path ?

No, because there is no requirement that every Uri map to a path on a filesystem that you can access.

Use getInputStream() on ContentResolver to get an InputStream on the Uri, and consume the data that way.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks a lot Mark...i got your point and it solved my problem – saurav Jan 07 '14 at 15:38
  • Just to add..opening of input steam on content resolver does not work when the file comes from apps like file manager and when file is on the sd card – saurav Jan 28 '14 at 16:06