-1

i want to have the Uri from a file path. This is the file path:

File path = new File(Environment.getExternalStorageDirectory().toString()+File.separator+"pic/picture.jpeg");

i tried in this way to retreive the uri:

Uri uri = Uri.fromFile(path);

but i tried also to make a log: Log.i("URI", "Uri dell' img " + uri + ":"); and the uri is still the file path! Not for example: "content://..." but "file://sdcard...."

How can i have the correct uri? Thanks

Atlas91
  • 5,754
  • 17
  • 69
  • 141

3 Answers3

1
public File getFileFromContentUri(final Uri uri) 
{
    if (uri == null || !(uri.getScheme().equals("content") || uri.toString().contains("file://"))) return null;

    if (uri.getScheme().equals("content")) {
        final String[] columns = new String[] {MediaStore.MediaColumns.DATA};
        final Cursor cursor = getContentResolver().query(uri, columns, null, null, null);
        if (cursor == null || cursor.getCount() == 0) return null;
        cursor.moveToFirst();
        final String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
        if (path != null) return new File(path);
        else return null;
   }
   else if (uri.toString().contains("file://")) {
       return new File(uri.getPath());
   }

   return null;
}
ecle
  • 3,952
  • 1
  • 18
  • 22
  • more http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework/20402190?noredirect=1#comment30507493_20402190 – ecle Sep 23 '14 at 07:55
0

use the method Uri.parse(path.getAbsolutePath()); this method will give you the Uri of the file Path

Mohammed Rampurawala
  • 3,033
  • 2
  • 22
  • 32
0
//write to this 

Uri photoUri = MediaStore.Images.Media.getContentUri("external");
  • and instead "external" have i to put the file path? – Atlas91 Sep 23 '14 at 07:19
  • can you please tell me which path you want ?? get image path from gallery or specific file path from sdcard ?? – Ramesh Devaiya Sep 23 '14 at 07:25
  • This way seems almost correct.. this is an uri. This is what i need but now it open the gallery in general and not the specific image..the path is this: File path = new File(Environment.getExternalStorageDirectory().toString()+File.separator+"pic/picture.jpeg"); – Atlas91 Sep 23 '14 at 07:26
  • actually with your way it brings me to: content://media/external/images/media: . So in general to the gallery. I need exactly to that picture – Atlas91 Sep 23 '14 at 07:31
  • yes. I have it but when i start the intent right now to open the image with gallery using uri, it opens the gallery in general.. i need open that image (picture.jpeg). – Atlas91 Sep 23 '14 at 07:35
  • pass intent to gallery and write code to get uri from that. – Dakshesh Khatri Sep 23 '14 at 07:35