我将本教程用作向我的应用程序添加功能的指南:
http://developer.android.com/training/camera/photobasics.html
教程附带的示例不完整,包含一些“错误”。我将错误这个词用引号引起来,因为涵盖了主要教程的目的:使用相机。
我正在专注于从拍摄的大照片中获取缩略图。当您运行该示例时,您会很快注意到,尽管大照片的缩略图已正确存储在指定的目录中,但大多数情况下并未显示该缩略图。
做了一些工作后我发现了以下“错误”:
1.- 图像的路径值经常丢失,因为 Activity 因内存不足而被破坏。我修复了在方法 onSaveInstanceState() 中存储照片路径的问题。
这样我总是能够访问我的图像,但它仍然没有出现。我继续做了一些测试,发现:
2.- 大多数时候,当要求 imageview 的测量值(宽度和高度)以重新缩放图像时,值是 0。我认为这可能是问题所在,但发现这是因为您无法获得测量值,直到 View 已绘制。所以我用一个处理程序修复了它并发送了一个延迟消息(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);
}
最佳答案
本教程也有很多问题,但我终于解决了。 我做了什么:
- 改变
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
通过
int scaleFactor = Math.max(photoW/targetW, photoH/targetH);
是关键。在此之前,我得到的是空白图像而不是图片。
- 我在 View 中放了一张默认图片。这可能不是一个包罗万象的答案,但我认为它无论如何都能提供更好的用户体验。
- 您可以使用 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); }
关于android - 使用 Android 教程方法重新缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15441328/