Android从相机获取图片后获取缩略图Uri

时间:2015-06-10 11:27:56

标签: android uri thumbnails android-camera-intent

如果我可以提供帮助,我不想使用任何位图调整大小代码。 当我从图库中获取图片时,我有一种获取MINI缩略图的方法,但是当我尝试使用相机时,它不起作用:

以下是方法:

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
}

以下是我为相机应用提供额外内容的方法:

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

获取Uri的两种方法(使用内容解析器或创建我的自定义Uri,不起作用,我永远无法获得缩略图。有没有办法指示系统生成缩略图?是缩略图总是生成?。

编辑:我想我没有清楚解释。我需要2个Uri:一个用于大图像,一个用于缩略图。我不需要位图。我使用Universal Image Loader来显示图像,所以我不需要小位图。我自己为大图像生成了Uri但是我想找到一种方法让Android生成缩略图,因为它已经为画廊的图片做了。

2 个答案:

答案 0 :(得分:0)

我有这段代码可能对你有所帮助,让我告诉你这段代码将调用相机并以完整分辨率拍摄存储在外部目录中的图片,你可以随时参考这条路径。

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

希望这会对你有所帮助。

答案 1 :(得分:0)

如果您只对缩略图感兴趣,请按以下步骤启动意图:

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

然后在OnActivityResult中,您可以从意图中提取拇指,如:

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