rbrlnx
rbrlnx

Reputation: 286

new File(uri.toString()) returns file not found

I have a URI and I want to create a file from this URI:

fileUri = file:///mnt/sdcard/20120904_162830.png;

File fichero = new File(fileUri.toString();

This works fine but when I try to create use or fichero.exist() = false

How can I create a file from this fileUri?

Solution:

fichero = new File(fileUri.getPath());//for camera
                    if (!fichero.exists()) {
                        fichero = new File(getRealPathFromURI(fileUri));//for gallery
                    }


public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

Upvotes: 3

Views: 3650

Answers (3)

Vishal Pawar
Vishal Pawar

Reputation: 4340

Here is solution

fileUri = "file:///mnt/sdcard/20120904_162830.png";

URI fUri = new URI(fileUri);

File fichero = new File(fileUri.getPath());

This works only if you are not giving any web address in fileUri

Upvotes: 0

MAC
MAC

Reputation: 15847

File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir,"20120904_162830.png");

add this permission to manifest...

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

EDIT :

String filename= output.getAbsolutePath();
filename= str.subString(str.indexOf("/"),str.length());

Upvotes: 0

Femi
Femi

Reputation: 64700

Try this:

File fichero = new File(fileUri.getPath());

Upvotes: 1

Related Questions