Get content uri from file path in android

android get pdf file path from content uri
android get file from absolute path
android content uri
get video path from uri android
android uri is not absolute
get real path from uri android
uri to bitmap android
get real path from uri android marshmallow

I know the absolute path of an image (say for eg, /sdcard/cats.jpg). Is there any way to get the content uri for this file ?

Actually in my code, I download an image and save it at a particular location. In order to set the image in an ImageView instance, currently I open the file using the path, get the bytes and create a bitmap and then set the bitmap in the ImageView instance. This is a very slow process, instead if I could get the content uri then I could very easily use the method imageView.setImageUri(uri)


Try with:

ImageView.setImageURI(Uri.fromFile(new File("/sdcard/cats.jpg")));

Or with:

ImageView.setImageURI(Uri.parse(new File("/sdcard/cats.jpg").toString()));

Convert a file path to Uri in Android, You can also get content uri using MediaStore database of Android. TEST (what returns Uri.fromFile and what returns MediaScannerConnection ): File videoFile  Because from android KitKat (sdk version 19), the system returned uri is not real local file path uri, it is a content provider style uri, so we should parse the uri and get the real file local path by query related content provider (image provider, audio provider, video provider and document provider).


UPDATE

Here it is assumed that your media (Image/Video) is already added to content media provider. If not then you will not able to get the content URL as exact what you want. Instead there will be file Uri.

I had same question for my file explorer activity. You should know that the contenturi for file only supports mediastore data like image, audio and video. I am giving you the code for getting image content uri from selecting an image from sdcard. Try this code, maybe it will work for you...

public static Uri getImageContentUri(Context context, File imageFile) {
  String filePath = imageFile.getAbsolutePath();
  Cursor cursor = context.getContentResolver().query(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
      new String[] { MediaStore.Images.Media._ID },
      MediaStore.Images.Media.DATA + "=? ",
      new String[] { filePath }, null);
  if (cursor != null && cursor.moveToFirst()) {
    int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
    cursor.close();
    return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
  } else {
    if (imageFile.exists()) {
      ContentValues values = new ContentValues();
      values.put(MediaStore.Images.Media.DATA, filePath);
      return context.getContentResolver().insert(
          MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    } else {
      return null;
    }
  }
}

File Path to Android Content URI, credit to http://stackoverflow.com/a , File Path to Android Content URI, credit to Took forever to find. Useful for Returns the Uri which can be used to delete/work with images in the photo gallery. Uri provided by FileProvider can be used also providing Uri for sharing files with other apps too. To get File Uri from a absolute path of File you can use DocumentFile.fromFile (new File (path, name)), it’s added in Api 22, and returns null for versions below.


// This code works for images on 2.2, not sure if any other media types

   //Your file path - Example here is "/sdcard/cats.jpg"
   final String filePathThis = imagePaths.get(position).toString();

   MediaScannerConnectionClient mediaScannerClient = new
   MediaScannerConnectionClient() {
    private MediaScannerConnection msc = null;
    {
        msc = new MediaScannerConnection(getApplicationContext(), this);
        msc.connect();
    }

    public void onMediaScannerConnected(){
        msc.scanFile(filePathThis, null);
    }


    public void onScanCompleted(String path, Uri uri) {
        //This is where you get your content uri
            Log.d(TAG, uri.toString());
        msc.disconnect();
    }
   };

How to convert absolute path to URI in android java, Can someone tell me how to convert this filepath to URI? API :- public String getRealPathFromURI(Uri contentUri) { // can post image String  Uri.fromFile (file).toString () only returns something like file///storage/emulated/0/* which is a simple absolute path of a file on the sdcard but with file// prefix (scheme) You can also get content uri using MediaStore database of Android TEST (what returns Uri.fromFile and what returns MediaScannerConnection):


The accepted solution is probably the best bet for your purposes, but to actually answer the question in the subject line:

In my app, I have to get the path from URIs and get the URI from paths. The former:

/**
 * Gets the corresponding path to a file from the given content:// URI
 * @param selectedVideoUri The content:// URI to find the file path from
 * @param contentResolver The content resolver to use to perform the query.
 * @return the file path as a string
 */
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;
}

The latter (which I do for videos, but can also be used for Audio or Files or other types of stored content by substituting MediaStore.Audio (etc) for MediaStore.Video):

/**
 * Gets the MediaStore video ID of a given file on external storage
 * @param filePath The path (on external storage) of the file to resolve the ID of
 * @param contentResolver The content resolver to use to perform the query.
 * @return the video ID as a long
 */
private long getVideoIdFromFilePath(String filePath,
        ContentResolver contentResolver) {


    long videoId;
    Log.d(TAG,"Loading file " + filePath);

            // This returns us content://media/external/videos/media (or something like that)
            // I pass in "external" because that's the MediaStore's name for the external
            // storage on my device (the other possibility is "internal")
    Uri videosUri = MediaStore.Video.Media.getContentUri("external");

    Log.d(TAG,"videosUri = " + videosUri.toString());

    String[] projection = {MediaStore.Video.VideoColumns._ID};

    // TODO This will break if we have no matching item in the MediaStore.
    Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(projection[0]);
    videoId = cursor.getLong(columnIndex);

    Log.d(TAG,"Video ID is " + videoId);
    cursor.close();
    return videoId;
}

Basically, the DATA column of MediaStore (or whichever sub-section of it you're querying) stores the file path, so you use that info to look it up.

android.net.Uri.getPath java code examples, if (cursor == null) { // Source is Dropbox or other similar local file path result = contentURI. Best Java code snippets using android.net.Uri.getPath (Showing top 20 results out of 4,185) Uri filePathFromActivity = (Uri) extras.get(Intent. /** * Gets the corresponding path to a file from the given content:// URI * @param selectedVideoUri The content:// URI to find the file path from * @param contentResolver The content resolver to use to perform the query.


You can use these two ways based on use

Uri uri = Uri.parse("String file location");

or

Uri uri = Uri.fromFile(new File("string file location"));

I have tried both ways and both works.

Retrieving file information, A file's data type indicates to the client app how it should handle the file's contents. To get the data type of a shared file given its content URI, the  A file's data type indicates to the client app how it should handle the file's contents. To get the data type of a shared file given its content URI, the client app calls ContentResolver.getType (). This method returns the file's MIME type. By default, a FileProvider determines the file's MIME type from its filename extension.


android - How to get the Full file path from URI, Try this. public String getRealPathFromURI(Uri contentUri) { String[] proj = { MediaStore.Audio.Media.DATA }; Cursor cursor = managedQuery(contentUri,  Get file path from uri android kotlin Share files val uri = FileProvider. The AAR file is an Android archive, which contains the compiled Kotlin source code and assets, required by Android to run an application using this SDK. Here I will introduce a simple way to clearly distinguish their differences and how to pick which to use.


How to get the Full file path from URI, Id like to get the full file path from a URI The URI isnt a Image but its a music file but if i do it like Uri contentUri) { string filename = ""; string thepath = ""; Android. We use cookies on our websites. Information about cookies and how you can object to the use of cookies at any time or end their use can be found in our privacy policy.


How To Get Real File Path From Android Uri, Get uri related content real local file path. */ private String getUriRealPath(​Context ctx, Uri uri) { String ret = ""; if( isAboveKitKat() ) { // Android OS  No Comments on Android gets the absolute path of the file according to the URI public class Uri2PathUtil { //Complex version processing (adapt to multiple APIs) public static String getRealPathFromUri ( Context context , Uri uri ) { int sdkVersion = Build .