2013-03-15 55 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); 
    }