2015-10-20 34 views
0

感謝您的閱讀。BitmapFactory.Decode返回null

[編輯] 如果我省略的選項,即,

Bitmap bmp = BitmapFactory.decodeFile(picturePath); 

然後,它返回一個位圖。

但與選項的說法,即

bmp = BitmapFactory.decodeFile(picturePath, options); 

返回null。

任何人都可以理解缺失或錯誤的選項?再次

謝謝,

[EDIT的端]

的目標是選擇圖像,如果需要的話它下采樣,保存它,然後將其加載到一個ImageView的。

BitmapFactory解碼總是返回null。 我確實有明顯的權限設置,路徑顯示完整.. /storage/emulated/0/aerg.png

請看下圖:

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) 
    { 

     Uri selectedImage = data.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 



     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     String imageType = options.outMimeType; 



     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 

     BitmapFactory.decodeFile(picturePath, options); 

     int inSampleSize = 1; 

     if (height > mheight || width > mWidth) 
     { 
      final int halfHeight = height/2; 
      final int halfWidth = width/2; 

      // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
      // height and width larger than the requested height and width. 
      while ((halfHeight/inSampleSize) > mheight 
        && (halfWidth/inSampleSize) > mWidth) 
      { 
       inSampleSize *= 2; 
      } 
     } 

     options.inSampleSize = inSampleSize; 

     Bitmap bmp = BitmapFactory.decodeFile(picturePath,options); 

     String downsampledPicturePath = saveImage(bmp, picturePath, "PrinterImages"); 

     Bitmap b = BitmapFactory.decodeFile(downsampledPicturePath); 
     iv.setImageBitmap(b); 

     //ImageView imageView = (ImageView) findViewById(R.id.theImageView); 
     //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

    } 
} 

private String saveImage(Bitmap bmp, String path, String folderName) 
{ 
    Context context = getApplicationContext(); 
    File folder = context.getDir(folderName, Context.MODE_PRIVATE); //Creating an internal dir; 

    String pictureName = path.substring(path.lastIndexOf("/") + 1); 
    String picture = new File(folder, pictureName).getAbsolutePath(); 

    FileOutputStream fos = null; 
    try 
    { 
     fos = new FileOutputStream(picture); 

     // Use the compress method on the BitMap object to write image to the OutputStream 
     bmp.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.close(); 
    } catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return picture; 

} 
+0

我已經在這幾天了,任何幫助表示讚賞! – Shaare

+0

好吧,我會paypal星巴克給任何人誰可以幫我解決這個問題。 – Shaare

回答

0

我就開始在入境doc文件爲decodeFile,因爲這顯然非常重要;

公共靜態位圖decodeFile(字符串的路徑名,BitmapFactory.Options選擇採用)

在API級別1 解碼文件路徑爲位圖。如果指定的文件名稱爲空,否則無法被解碼成位圖,該函數返回null

參數 pathName將完整的路徑名要解碼的文件 選擇採用空行;控制下采樣和圖像是否應該被完全解碼,或只是選項是尺寸 回。 返回 解碼的位圖,或NULL,如果圖像數據不能被解碼,或者,如果OPTS非空,如果選擇採用只請求返回的大小(以opts.outWidth和opts.outHeight)

我不太明白這一點說明,但在我看來,你從來沒有在設置的選項寬度或高度從看你的代碼嗎?我可能完全關閉這個雖然。

還要注意; http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

「public boolean inJustDecodeBounds If設置爲true,解碼器將返回空(無位圖),但出...「

我有點寫這種反應在旅途中。我認爲上面的報價是你的問題 - 你在代碼中設置爲true。

+0

嗨SuperHacker!我想通過這篇文章http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/瞭解到這一點,並且今晚將會在JDeveloDeBounds中閱讀更多關於這方面的內容。謝謝!請讓我知道我可以給你買一杯咖啡。這個社區太棒了。我不記得我在vb5的日子裏是如何管理的...... – Shaare

+0

您的感激之情和貼出的帖子是我需要的。此外,我確實認爲關於該評論的文章可能包含修復我目前正在開發的應用程序的縮放問題的代碼 - 即縮放遊戲中的背景圖片,所以感謝發佈該內容 - 這給我造成了很多混淆 –