ITKeyword,专注技术干货聚合推荐

注册 | 登录

Android getting thumbnail Uri after getting picture from the Camera

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

I do not want to use any bitmap resizing code if I can help it. I have a method to get a MINI thumbnail uri when I get pictures from the Gallery but when I try to do the same with the Camera it does not work:

Here are the methods:

private Uri getParsedUri(Uri selectedImageUri) {
    Uri parsed = null;
    try {
        String uri = getThumbnailPath(selectedImageUri);

        if (uri != null) {
            Timber.d("Thumb uri found %s", uri);
            File file = new File(uri);
            parsed = Uri.fromFile(file);
            Timber.d("thumb uri parsed : %s", parsed);
        } else {
            Timber.d("Thumb uri not found, using :  %s", selectedImageUri);
        }
    } catch (Exception e){
        Timber.e(e, "Error getting mini uri");
    }
    return parsed;
}

private String getThumbnailPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media._ID };
    String result = null;
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    if (cursor != null && cursor.getCount() > 0) {
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media._ID);

        cursor.moveToFirst();
        long imageId = cursor.getLong(column_index);
        cursor.close();

        Cursor cursor2 = MediaStore.Images.Thumbnails.queryMiniThumbnail(
                getContentResolver(), imageId,
                MediaStore.Images.Thumbnails.MINI_KIND,
                null);
        if (cursor2 != null && cursor2.getCount() > 0) {
            cursor2.moveToFirst();
            result = cursor2.getString(cursor2.getColumnIndexOrThrow(MediaStore.Images.Thumbnails
                    .DATA));
            cursor2.close();
        }
    }
    return result; //always null for pictures from the Camera
}

Here is how I provide the uri extra for the Camera app:

private void onTakePhotoFromCameraClick() {
    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//        takePicUri = Uri.fromFile(getTempFile(ProductNewActivity.this));
    takePicUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new ContentValues());
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            takePicUri);
    startActivityForResult(intent, OPTION_TAKE_PHOTO_FROM_CAMERA);
}


private File getTempFile(Context context) {
     final File path = new  File(Environment.getExternalStorageDirectory(),
            context.getPackageName());
    if (!path.exists()) {
        path.mkdir();
    }
    long millis = new Date().getTime();
    String name_file = String.valueOf(millis);
    File tempImageFile = new File(path, "MyImage_" + name_file + ".tmp");
    return tempImageFile;
}

both of the methods of getting the Uri (either using content resolver or creating my custom Uri, do not work and I am never able to get a thumbnail. Is there a way to indicate the system to generate a thumbnail? Is the thumbnail always generated?.

Edit: I think I did not explain clearly. I need 2 Uri: one for big image and one for a thumbnail. I do not need the bitmaps. I am using Universal Image Loader to display the images so I do not need the small bitmap. I generate the Uri for the big image myself but I would like to find a way for Android to generate the thumbnail since it already does it for pictures of the gallery.

android uri thumbnails android-camera-intent
|
  this question
edited Jun 10 '15 at 13:00 asked Jun 10 '15 at 11:27 vallllll 1,441 2 25 62      Why vote down? Could you please explain why you voted down so I can correct what is wrong? –  vallllll Jun 10 '15 at 11:35

 | 

2 Answers
2

I have this piece of code which may helpful for you,Let me tell you this code will invoke camera and take a picture stored in external directory with full resolution and you can refer this path any time.

final int TAKE_PHOTO_REQ = 100;
    String file_path = Environment.getExternalStorageDirectory()
            + "/recent.jpg";
    file = new File(file_path);
public void takePic(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
}
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PHOTO_REQ: {
            if (resultCode == TakePicture.RESULT_OK && data != null) {

                Bitmap myBmp = (Bitmap) data.getExtras().get("data");



                imageView.setImageBitmap(myBmp);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();

                // you can create a new file name "recent.jpg" in sdcard folder.
                File f = new File(file_path);
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // write the bytes in file
                FileOutputStream fo = null;
                try {
                    fo = new FileOutputStream(f);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    fo.write(bytes.toByteArray());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // remember close de FileOutput
                try {
                    fo.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.e("take-img", "Image Saved to sd card...");

                break;
            }
        }
        }
    }

Hope this will help you.


|
  this answer
answered Jun 10 '15 at 11:38 sahulab 568 1 16      I do not want a bitmap, I want an Uri for small images. –  vallllll Jun 10 '15 at 11:39      Try to get the uri from the path itself.@vallllll –  sahulab Jun 10 '15 at 11:42      Yes but it creates an extra file and an extra bitmap which I do not need. I was wondering if there was a way to get the thumbnail that Android creates by default. –  vallllll Jun 10 '15 at 13:02      Hope we will get the solution soon thanks @vallllll –  sahulab Jun 10 '15 at 13:24

 | 

If you are interested only in a thumbnail then start the intent as follows:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_THUMB );

Then in OnActivityResult you can extract the thumb from the intent like:

Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
imageView1.setImageBitmap(bitmap);

|
  this answer
answered Jun 10 '15 at 12:38 greenapps 6,053 2 6 13      Thanks but on Samsung Galaxu S5 I get null extra –  vallllll Jun 10 '15 at 14:56

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。