2016-12-28 37 views
0

我在按鈕創建的活動,可以選擇照片從畫廊(意圖),並調整大小挑選照片,並把(在可繪製對象)錯誤的位圖改​​變大小在某些設備

活動的一些方法:

protected void onActivityResult(int requestCode, int resultCode, 
           Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
     case PICK_PHOTO_FOR_AVATAR: 
      if(resultCode == RESULT_OK){ 
       Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex); 
       cursor.close(); 
       Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 

       try{ 

        if(density>=4) {//xxxdpi 
         setButtonIcon(Photos.changeSize(yourSelectedImage, 196, 196), tempButton); 
        }else if (density < 4 && density >= 3) {//xxdpi 
         setButtonIcon(Photos.changeSize(yourSelectedImage, 180, 180), tempButton); 
         Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show(); 
        } else if (density < 3 && density >= 2) {//xdpi 
         setButtonIcon(Photos.changeSize(yourSelectedImage,96,96), tempButton); 
         Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show(); 
        } else if (density < 2 && density >= 1.5) {//hdpi 
         setButtonIcon(Photos.changeSize(yourSelectedImage,72,72), tempButton); 
         Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show(); 
        } else if (density < 1.5 && density >= 1) {//mdpi 
         setButtonIcon(Photos.changeSize(yourSelectedImage,48,48), tempButton); 
         Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show(); 
        } else if (density < 1 && density >= 0.75) {//ldpi 
         setButtonIcon(Photos.changeSize(yourSelectedImage,36,36), tempButton); 
         Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show(); 
        } 
       }catch (Exception e){ 
         Toast.makeText(context,density+" "+e.getMessage(),Toast.LENGTH_SHORT).show(); 
       } 


      } 
      break; 

    } 
} 

private void setButtonIcon(Bitmap icon,DocumentButton btn) { 
    Drawable draw=new BitmapDrawable(getResources(),icon); 
    btn.setCompoundDrawablesWithIntrinsicBounds(null, draw , null, null); 
} 

它可以在某些設備,但是當我在三星Galaxy S6測試(含4.0密度)我得到這個錯誤

試圖調用虛擬方法INT android.graphics.Bitmap.getWidth()

此錯誤快到時,我想調整的位圖圖片:

public static Bitmap changeSize(Bitmap bitmap,int newWidth,int newHeight)throws Exception{ 

    return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false); 

} 

我怎樣才能解決這個問題?

+0

您必須將圖像大小調整爲特定的高度和寬度或動態? –

+0

我無法理解你的問題。你檢查所有的方法? – b4hr4m

回答

0

我解決我的問題,我改變的代碼只是一些線條,我認爲光標是我的問題,在android系統5和上,光標不能得到回升圖像路徑(通路結果= NULL)

更新行:

final Uri imageUri = imageReturnedIntent.getData(); 
final InputStream imageStream = getContentResolver().openInputStream(imageUri); 
final Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); 
相關問題