0

I have a simple application that take a picture and store it in a folder called CameraSample on the sd card. to do that I use MediaStore.EXTRA_OUTPUT option as extra:

    private void dispatchTakePictureIntent(int actionCode) {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File f = null;

        try {
            f = setUpPhotoFile();
            mCurrentPhotoPath = f.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        } catch (IOException e) {
            e.printStackTrace();
            f = null;
            mCurrentPhotoPath = null;
        }
        if (takePictureIntent.resolveActivity(getPackageManager()) != null){
            startActivityForResult(takePictureIntent, actionCode);
        }
    }

The immage is taken and stored correctly. Now I would like to show thumbnails of the images I took, so I look for all the images, I take into account just the ones that are under CameraSample album and I want to retrieve the thumbnails, but the result is that there are no thumbnails for the images I take this way, is that because I use the MediaStore.EXTRA_OUTPUT?

    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.MINI_THUMB_MAGIC};

    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

    ArrayList<PhotoItem> result = new ArrayList<PhotoItem>();
    Uri fullImageUri;
    String album_id;
    String album_name;
    if (cursor != null) {
        while (cursor.moveToNext()) {

            int column_idx = cursor.getColumnIndex(MediaStore.Images.Media._ID);
            album_id = cursor.getString(column_idx);

            column_idx = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            album_name = cursor.getString(column_idx);
            if(album_name.equalsIgnoreCase("CameraSample")){
                column_idx = cursor.getColumnIndex(projection[1]);
                String filePath = cursor.getString(column_idx);
                fullImageUri = Uri.parse(filePath);
                Uri thumbnailUri = uriToThumnailImage(cursor, context);
                PhotoItem newItem = new PhotoItem(thumbnailUri,fullImageUri);
                result.add(newItem);
            }
        }
    }
    cursor.close();

Where uriToThumbnailImage looks like:

private static Uri uriToThumnailImage(Cursor cursor, Context context){
    String imageId = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID));

    // Request image related to this thumbnail
    String[] filePathColumn = { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID};
    Cursor imagesCursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, filePathColumn, MediaStore.Images.Thumbnails.IMAGE_ID + "=?", new String[]{imageId}, null);

    if (imagesCursor != null && imagesCursor.moveToFirst()) {
        int columnIndex = imagesCursor.getColumnIndex(filePathColumn[0]);
        String filePath = imagesCursor.getString(columnIndex);
        imagesCursor.close();
        return Uri.parse(filePath);
    } else {
        imagesCursor.close();
        return Uri.parse("");
    }
}
Shilaghae
  • 957
  • 12
  • 22

1 Answers1

0

http://developer.android.com/reference/android/media/MediaScannerConnection.html

Run the media scanner over the newly created content.

danny117
  • 5,581
  • 1
  • 26
  • 35
  • I forgot to say that in onActivityResult I do Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");File f = new File(mCurrentPhotoPath);Uri contentUri = Uri.fromFile(f);mediaScanIntent.setData(contentUri);this.sendBroadcast(mediaScanIntent); That should be the same that usinf MediaScannerConnection, shouldn't it? – Shilaghae Oct 24 '14 at 17:00