简体   繁体   中英

Android get Media.Images Uri from ID

I am new to Android and Java and this week I've been doing a self-taught crash course. So far what I've learned has not been too complicated as I've already built a number of years of coding experience. So, background history out of the way, onto my question.

The below code is two functions I wrote to take an image ID from a database and parse the correct Uri which I can then use to upload the photo to a website. So could you kind folks look over my code and let me know if I'm doing a terrible job or if I am heading in the right direction or even if there is a better/native way to do what I need.

Also, note: the below code does work. I just don't know if it is the right way to do it.

Thanks!

// Usage Map idPath = ImageIdPathFetcher.getRealIdPathFromID(getApplicationContext(), Integer.valueOf(image_id));

    public static Map getRealIdPathFromID(Context context, Integer id) {
        Map<String,String> idPath = new HashMap<String, String>();

        Uri external_images_uri = MediaStore.Images.Media.getContentUri("external");
        Uri internal_images_uri = MediaStore.Images.Media.getContentUri("internal");

        // initialize uri
        Uri uri = external_images_uri;

        String ext_img_uri = external_images_uri.toString()+"/"+id;
        String int_img_uri = internal_images_uri.toString()+"/"+id;

        if(check_uri(context, ext_img_uri))
        {
            uri = Uri.parse(ext_img_uri);
        }else if(check_uri(context, int_img_uri))
        {
            uri = Uri.parse(int_img_uri);
        }else {
            idPath.put("id", "");
            idPath.put("path", "");
            return idPath;
        }

        String[] proj       = { Media.DATA, Media._ID };
        Cursor cursor       = context.getContentResolver().query(uri, proj, null, null, null);
        int column_index    = cursor.getColumnIndexOrThrow(Media.DATA);
        cursor.moveToFirst();
        String filepath = cursor.getString(column_index);
        idPath.put("id",   id.toString());
        idPath.put("path", filepath);
        return idPath;
    }

    public static boolean check_uri(Context context, String uri)
    {
        try{
            ContentResolver cr = context.getContentResolver();
            String[] projection = {MediaStore.MediaColumns.DATA};
            Cursor cur = cr.query(Uri.parse(uri), projection, null, null, null);
            if(cur != null)
            {
                cur.moveToFirst();
                String filePath = cur.getString(0);
                if(! new File(filePath).exists()){
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e)
        {
            return false;
        }
        return true;
    }

This is what I use

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) {
        try {
            if (cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
            }
        } finally {
            cursor.close();
        }
    }
    cursor = context.getContentResolver().query(
            MediaStore.Images.Media.INTERNAL_CONTENT_URI,
            new String[]{MediaStore.Images.Media._ID},
            MediaStore.Images.Media.DATA + "=? ",
            new String[]{filePath}, null);
    if (cursor != null) {
        try {
            if (cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                return Uri.withAppendedPath(MediaStore.Images.Media.INTERNAL_CONTENT_URI, String.valueOf(id));
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

It is possible to query both internal and external MediaStore database also with MergeCursor :

String myIdImgStr = "123"; // the unique ID of the image in the MediaStore
ContentResolver contentResolver = this.getContext().getContentResolver();
String [] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };

MergeCursor cursor = new MergeCursor(new Cursor[] {
  MediaStore.Images.Media.query(contentResolver,
    Uri.parse(MediaStore.Images.Media.INTERNAL_CONTENT_URI + "/" + myIdImgStr),
    proj),
  MediaStore.Images.Media.query(contentResolver,
    Uri.parse(MediaStore.Images.Media.EXTERNAL_CONTENT_URI + "/" + myIdImgStr),
    proj)
});

if(cursor.getCount() == 1) {
  cursor.moveToFirst();
  String filepath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM