2017-02-17 1 views
0

Wenn ich ein Bild auswählen, gibt die BitmapFactory.decodeFile null zurück. Hier ist meine Funktion:Probleme mit BitmapFactory.decodeFile, wenn ich ein Bild auswählen

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

    // Here we need to check if the activity that was triggers was the Image Gallery. 
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value. 
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked. 
    if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) { 
     // Let's read picked image data - its URI 
     Uri pickedImage = data.getData(); 
     // Let's read picked image path using content resolver 
     String[] filePath = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null); 
     cursor.moveToFirst(); 
     String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap bitmap = BitmapFactory.decodeFile(imagePath,options); 
     //imageView.setImageBitmap(bitmap); 

     // Do something with the bitmap 


     // At the end remember to close the cursor or you will end with the RuntimeException! 
     cursor.close(); 
    } 

} 

Kann jemand mir sagen, was das Problem ist?

+0

Können Sie genauer sein, was Sie versuchen zu erreichen – vishnumm93

+0

Ich versuche, ein Bild aus der Galerie und holen Sie es Bitmap – user2199630

Antwort

0

String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

wollen Sie das filePath in der Variablen imagePath haben? Oder ist der imagePath-Wert auch null?

UPDATE
Ihre BitmapFactory.decodeFile() kehrt null, da kann der image zu groß ist und die eine Ausnahme zu werfen.
überprüfen Sie Ihr Protokoll und sehen Sie, ob es outOfMemory Bitmap Fehler gibt es gibt. Lassen Sie es mich wissen

+0

Ja, ich habe einen imagePath und das ist ein Beispiel für einen Pfad, den ich bekomme:/storage/emuliert/0/WhatsApp/Media/WhatsApp Bilder/IMG-20170217-WA0001.jpg – user2199630

+0

Ich erhalte diesen Fehler:/BitmapFactory: Kann Stream nicht decodieren: java.io.FileNotFoundException:/storage/emuliert/0/WhatsApp/Media/WhatsApp Bilder/IMG-20170217-WA0001.jpg: Öffnen fehlgeschlagen: Aber ich habe diese Zeile im Manifest: user2199630

+0

versuchen Sie in der API-Ebene> = 23? –

Verwandte Themen