2017-10-16 64 views
0

我在我的片段我的圖片加載有問題,當我選擇的地方搜索圖像,並挑選的圖片,而不是它,我得到的黑圈,這是我的代碼:我收到黑屏,而不是照片

civ = view.findViewById(R.id.circle_profile); 

civ.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE); 
    } 
}); 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getActivity().getContentResolver().query(selectedImage,filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
     CircleImageView imageView = getActivity().findViewById(R.id.circle_profile); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
    } 
} 

XML:

<de.hdodenhof.circleimageview.CircleImageView 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:layout_column="0" 
    android:layout_row="1" 
    android:layout_rowSpan="3" 
    android:layout_marginLeft="30dp" 
    android:layout_columnWeight="1" 
    android:id="@+id/circle_profile" 
    android:src="@drawable/profile" 
    app:civ_border_color="#FFFFFF" 
    app:civ_border_width="2dp" 
    /> 

它看起來像邊框顏色只剩

+0

你檢查picturePath值嗎? –

+0

對我來說它看起來很好 –

+0

你可以使用圖像選擇器庫,例如https://github.com/esafirm/android-image-picker – Expiredmind

回答

0

我懷疑,在你的代碼: BitmapFactory.decodeFile(picturePath)回報null也許是因爲圖像的尺寸是大

我建議使用BitmapFactory.Options並用BitmapFactory.Options通過它內部decodeFile功能作爲第二個參數。

編輯:

BitmapFactory.Options可以像下面的代碼:

// Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 


    BitmapFactory.decodeFile(picturePath, bmOptions); 

int photoW= bmOptions.outWidth; 
int photoH = bmOptions.outHeight; 

// Get the dimensions of the View 
    int targetW = imageView.getWidth(); 
    int targetH = imageView.getHeight(); 

//Determine how much to scale down the image 
int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

//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(picturePath, bmOptions); 
imageView.setImageBitmap(bitmap); 

希望它能幫助!

+0

我應該給作爲一個選項? –

+0

我已經用'BitmapFactory.Options'編輯我的答案代碼 –

+0

picturePath我應該這樣寫嗎? 「」@ drawable/profile「 –

相關問題