2017-10-16 6 views
0

je suis arrivé problème avec mon chargement d'images dans mon Fragment, quand je choisi endroit pour rechercher l'image et choisir l'image au lieu de j'obtenir le cercle noir, voici mon code:je reçois un écran noir au lieu de photo

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" 
    /> 

Il est ressemble à la couleur de la bordure gauche ne

+0

Avez-vous vérifié la valeur de PicturePath? –

+0

Pour moi son aspect bien

+0

vous pouvez utiliser les bibliothèques de sélecteur d'image par exemple https://github.com/esafirm/android-image-picker – Expiredmind

Répondre

0

Je pense que dans votre code: BitmapFactory.decodeFile(picturePath) retours null peut-être parce que la taille de l'image est grande

Je suggère d'utiliser BitmapFactory.Options et passe à l'intérieur decodeFile fonction avec BitmapFactory.Options comme second paramètre.

EDIT:

BitmapFactory.Options peut avoir le code comme ci-dessous:

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

Hope it helps!

+0

Et que dois-je donner en option? –

+0

J'ai édité ma réponse avec le code 'BitmapFactory.Options' –

+0

picturePath devrais-je écrire comme ceci? "" @ drawable/profile " –