How to get a Uri object from Bitmap

后端 未结 7 1619
醉话见心
醉话见心 2020-12-09 15:55

On a certain tap event, I ask the user to add an image. So I provide two options:

  1. To add from gallery.
  2. To click a new image from camera.
相关标签:
7条回答
  • 2020-12-09 17:00

    I tried the following code snippet from the post I mentioned in my comment.. and it's working fine for me.

    /**
     * Gets the last image id from the media store
     * 
     * @return
     */
    private int getLastImageId() {
        final String[] imageColumns = { MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA };
        final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
        Cursor imageCursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
                null, null, imageOrderBy);
        if (imageCursor.moveToFirst()) {
            int id = imageCursor.getInt(imageCursor
                    .getColumnIndex(MediaStore.Images.Media._ID));
            String fullPath = imageCursor.getString(imageCursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));
            Log.d(getClass().getSimpleName(), "getLastImageId::id " + id);
            Log.d(getClass().getSimpleName(), "getLastImageId::path "
                    + fullPath);
            imageCursor.close();
            return id;
        } else {
            return 0;
        }
    }
    

    OutPut in logcat:

    09-24 16:36:24.500: getLastImageId::id 70
    09-24 16:36:24.500: getLastImageId::path /mnt/sdcard/DCIM/Camera/2012-09-24 16.36.20.jpg
    

    Also I don't see any harcoded names in the above code snippet. Hope this helps.

    0 讨论(0)
提交回复
热议问题