【问题标题】:Rescaling a image with Android tutorial method使用 Android 教程方法重新缩放图像
【发布时间】:2013-03-04 16:39:41
【问题描述】:

我使用本教程作为向我的应用添加功能的指南:

http://developer.android.com/training/camera/photobasics.html

本教程附带的示例不完整,包含一些“错误”。我将错误一词放在引号中,因为主要教程的目的已经涵盖:使用相机。

我正专注于从拍摄的大照片中获取缩略图。当您运行该示例时,您会很快注意到,虽然大照片的缩略图已正确存储在指定的目录中,但大多数情况下都不会显示该缩略图。

做了一些工作我发现了以下“错误”:

1.- 图像的路径值经常丢失,因为活动因内存不足而被破坏。我修复了在 onSaveInstanceState() 方法中存储照片路径的问题。

这样我总是能够访问我的图像,但它仍然没有出现。我继续进行一些测试并发现:

2.- 大多数情况下,当要求图像视图的度量(宽度和高度)以重新缩放图像时,值是 0。我认为这可能是问题并发现它是因为您无法获得度量直到视图已绘制。所以我用一个处理程序修复了这个问题,并发送了一条延迟消息(1.5'')来执行。现在,测量值总是正确的,但即使缩略图大部分时间都没有显示

所以我认为 Bitmap.decodeFile 方法返回一个空值,尽管所有变量都设置正确。但事实并非如此,它正在返回一个位图。所以,伙计们和女孩们,我知道我无法找到不显示缩略图的原因。

非常感谢您的帮助。谢谢!

这是重新缩放图像的方法:

//Scaling the real size photo to the image view size
private void setImagenPequena()
{
    Log.w("PAth: ", n_path_foto_actual);
     // Get the dimensions of the View
    int targetW = n_iv_foto.getMeasuredWidth();
    int targetH = n_iv_foto.getMeasuredHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(n_path_foto_actual, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    Log.w("setImagenPequena: ", "photoW: " + Integer.toString(photoW));
    Log.w("setImagenPequena: ", "photoH: " + Integer.toString(photoH));
    Log.w("setImagenPequena: ", "targetW: " + Integer.toString(targetW));
    Log.w("setImagenPequena: ", "targetH: " + Integer.toString(targetW));

    if(targetW > 0 && targetH > 0)
    {
    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
    bmOptions.inSampleSize = scaleFactor;
    }
    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(n_path_foto_actual, bmOptions);

    if(bitmap == null)
        Log.w("valor bitmap: ", "null");
    else
        Log.w("valor bitmap: ", "!=null");
    n_iv_foto.setImageBitmap(bitmap);

}

【问题讨论】:

    标签: android android-intent thumbnails android-camera


    【解决方案1】:

    我在本教程中也遇到了很多问题,但我终于解决了。 我做了什么:

    • 改变

    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    通过

    int scaleFactor = Math.ma​​x(photoW/targetW, photoH/targetH);

    是关键。在此之前,我得到的是空白图像而不是图片。

    • 我在视图中放置了一张默认图片。这可能不是一个全面的答案,但我认为无论如何它可以提供更好的用户体验。
    • 您可以使用http://blog-emildesign.rhcloud.com/?p=590 获取一个有效的decodeSampledBitmapFromFile 示例。
    • 否则在这里使用我的一些代码:

      WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
      Display display = wm.getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      mWidth = size.x;
      mHeight = size.y;
      ...
      
      private void setPic() {
      
              int targetW = mWidth;
              int targetH = mHeight;
              // Get the dimensions of the bitmap
              BitmapFactory.Options bmOptions = new BitmapFactory.Options();
              bmOptions.inJustDecodeBounds = true;
      
              BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
              int photoW = bmOptions.outWidth;
              int photoH = bmOptions.outHeight;
      
              // Determine how much to scale down the image
              int scaleFactor = Math.max(photoW/targetW, photoH/targetH);
              bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
              // Decode the image file into a Bitmap sized to fill the View
              bmOptions.inJustDecodeBounds = false;
              bmOptions.inSampleSize = scaleFactor;
              bmOptions.inPurgeable = true;
      
              Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,                 
              bmOptions);
              mImageView.setImageBitmap(bitmap);
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      相关资源
      最近更新 更多