如何从Android相机获取缩略图和图像路径?

时间:2013-08-12 09:22:12

标签: android android-intent

通过这种方式

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, REQUEST_CODE);

我通过这种方式在onActivityResult而不是路径中得到位图形式的意图

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(intent, REQUEST_CODE);

我可以从intent获取路径,而不是位图!!,我如何从android摄像头获取位图(缩略图)和路径?

2 个答案:

答案 0 :(得分:4)

通过使用该路径可以获得图像路径,创建像这样的缩略图

/**
 * 
 * Returns the given size of the bitmap
 * 
 * @param path
 * @return {@link Bitmap}
 */
private Bitmap getThumbnailBitmap(String path, int thumbnailSize) {
    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
        return null;
    }
    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / thumbnailSize;
    return BitmapFactory.decodeFile(path, opts);
}

给出你想要的大小

答案 1 :(得分:0)

试试这个代码,我在应用程序中使用了这个,这将完美无缺,

static final int CAMERA_REQUEST = 1888;
String name =   dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
File destination = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
destination = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
startActivityForResult(cameraIntent, CAMERA_REQUEST);

在你的OnActivityResult()

Bundle bundle = data.getExtras();
    if(bundle != null){
    if (requestCode == CAMERA_REQUEST) { 
       Bitmap photo = (Bitmap) data.getExtras().get("data");

       //Here photo is your bitmap image, use this as per your requirement