Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Uri from real Path

Tags:

android

I have a real path of a file like "file:///mnt/sdcard/3dphoto/temp19.jps" , how can i get the uri like "content://media/external/images/media/1 "?

like image 493
newstartgirls Avatar asked Oct 30 '11 12:10

newstartgirls


People also ask

How get URI from Filepath?

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. Show activity on this post. Uri uri = Uri.

How can I get Uri video on Android?

Use openInputStream() on ContentResolver to get an InputStream on the content identified by the Uri . Then, either: Use that stream directly with your preferred HTTP client API to upload the content, or. Use that stream to make a local file copy of the content, then use that file with your preferred HTTP client.

What is Uri parse in android?

It is an immutable one-to-one mapping to a resource or data. The method Uri. parse creates a new Uri object from a properly formated String .

What is a URI Kotlin?

kotlin.Any. ↳ java.net.URI. Represents a Uniform Resource Identifier (URI) reference.


2 Answers

Transform your "file://..." in a file path, find the id of the item with the following code, and then append it to provider URI. In addition, based on file extensions, use the right provider (for example MediaStore.Video.Media.EXTERNAL_CONTENT_URI or MediaStore.Image.Media.EXTERNAL_CONTENT_URI)

/**
 * Given a media filename, returns it's id in the media content provider
 *
 * @param providerUri
 * @param appContext
 * @param fileName
 * @return
 */
public long getMediaItemIdFromProvider(Uri providerUri, Context appContext, String fileName) {
    //find id of the media provider item based on filename
    String[] projection = { MediaColumns._ID, MediaColumns.DATA };
    Cursor cursor = appContext.getContentResolver().query(
            providerUri, projection,
            MediaColumns.DATA + "=?", new String[] { fileName },
            null);
    if (null == cursor) {
        Log.d(TAG_LOG, "Null cursor for file " + fileName);
        return ITEMID_NOT_FOUND;
    }
    long id = ITEMID_NOT_FOUND;
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        id = cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID));
    }
    cursor.close();
    return id;
}

Sometimes MediaProvider doesn't refresh immediatly after one media file is added to device's storage. You can force to refresh its records using this method:

/**
 * Force a refresh of media content provider for specific item
 * 
 * @param fileName
 */
private void refreshMediaProvider(Context appContext, String fileName) {
    MediaScannerConnection scanner = null;
    try {
        scanner = new MediaScannerConnection(appContext, null);
        scanner.connect();
        try {
            Thread.sleep(200);
        } catch (Exception e) {
        }
        if (scanner.isConnected()) {
            Log.d(TAG_LOG, "Requesting scan for file " + fileName);
            scanner.scanFile(fileName, null);
        }
    } catch (Exception e) {
        Log.e(TAG_LOG, "Cannot to scan file", e);
    } finally {
        if (scanner != null) {
            scanner.disconnect();
        }
    }
} 
like image 52
Rainbowbreeze Avatar answered Sep 21 '22 07:09

Rainbowbreeze


I had same question for my file explorer activity...but u should knw tht the contenturi for file only supports the mediastore data like image,audio and video....I am giving you for getting image content uri from selecting an image from sdcard....try this code...may be 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));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + 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;
            }
        }
    }
like image 31
Jinal Jogiyani Avatar answered Sep 18 '22 07:09

Jinal Jogiyani