【问题标题】:How to get thumbnail and image path from android camera?如何从android相机获取缩略图和图像路径?
【发布时间】:2013-08-13 13:23:27
【问题描述】:

这样

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

我可以从意图中获取路径,而不是位图!!,我如何从 android 相机中获取位图(缩略图)和路径?

【问题讨论】:

标签: android android-intent


【解决方案1】:

从中您可以获取图像路径,使用该路径创建这样的缩略图

/**
 * 
 * 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);
}

给出你想要的尺寸

【讨论】:

  • mmmmm 很好,谢谢你的回答.. :) 但是那个 thumbnailSize 参数怎么样?那是什么 ?是关于宽度和高度的吗?
  • 如果我想要一个 100dp x 100dp 的图像缩略图,我必须设置什么参数???????????
  • 我已将拇指大小设置为 400 并返回空白
【解决方案2】:

试试这个代码,我已经在一个应用程序中使用过它,它会完美运行,

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

【讨论】:

  • 我们没有得到图像路径?
  • 现在我仍在尝试@kalyanpvs 的解决方案,我稍后再试.. :)
  • 我的 data 为空(在 onActivityResult 内)。 :(
猜你喜欢
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
  • 2015-08-25
  • 1970-01-01
相关资源
最近更新 更多