2013-03-15 56 views
2

我使用本教程为指针增加一个功能,我的应用程序:重新缩放与Android教程方法的图像

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

与教程来,是不完整的包含一些“错误”的例子。我将引用错误的单词放在引号中,因为主要教程的purpouse包含在内:使用相机。

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

做了一些工作,我发现下面的“错误”:

1.-图像的路径值是经常丢失,因为活动被摧毁,由于内存不足。我修复了在方法onSaveInstanceState()中存储照片的路径。

这种方式我总是能够访问我的图像,但它仍然没有出现。我继续做一些测试,发现:

2.-大多数时候,当要求imageview的措施(宽度和高)重新缩放图像的值为0我认为这可能是问题,并发现它是因为在绘制视图之前无法获得措施。所以我用一个处理程序修正了这个问题,并发送一个延迟的消息(1.5'')来执行。现在,度量总是可以正确获得,但即使缩略图在大多数时间不显示

所以我认为Bitmap.decodeFile方法返回null值,尽管所有的变量都设置正确。但事实并非如此,它正在返回一个位图。所以男人和女孩,我承认我无法找到为什么不显示缩略图。

有点帮助将不胜感激。谢谢!

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

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

} 

回答

1

我有许多问题,以及与此教程,但我终于固定它。 我所做的:

  • 变化

INT比例因子=数学。 分钟(photoW/targetW,photoH/targetH);

通过

INT比例因子=数学。 (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); 
    }