newstartgirls
newstartgirls

Reputation: 281

Get Uri from real Path

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 "?

Upvotes: 7

Views: 13294

Answers (3)

Alireza Sobhani
Alireza Sobhani

Reputation: 777

Another easy way:

File file = new File(Path);

Uri uri = Uri.fromFile(file);

Upvotes: 1

Jinal Jogiyani
Jinal Jogiyani

Reputation: 1209

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;
            }
        }
    }

Upvotes: 6

Rainbowbreeze
Rainbowbreeze

Reputation: 1503

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();
        }
    }
} 

Upvotes: 6

Related Questions