2017-02-17 3 views
0

Когда я выбираю картинку, BitmapFactory.decodeFile возвращает null. Вот моя функция:Проблемы с BitmapFactory.decodeFile, когда я выбираю картинку

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(); 
    } 

} 

Может кто-нибудь сказать мне, что это проблема?

+0

Можете ли вы уточнить, что вы пытаетесь достичь – vishnumm93

+0

Я пытаюсь выбрать картинку из галереи и получить ее растровое изображение – user2199630

ответ

0

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

вы имеете Filepath в переменной ImagePath? или значение imagePath равно null?

UPDATE
ваш BitmapFactory.decodeFile() возвращается нуль, потому что может быть IMAGESIZE слишком велик и его метания некоторое исключение.
проверить ваш журнал и посмотреть, есть ли outOfMemory Bitmap ошибка есть. дайте мне знать

+0

Да, у меня есть ImagePath, и это пример пути, который я получаю:/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170217-WA0001.jpg – user2199630

+0

Я получаю эту ошибку:/BitmapFactory: Невозможно декодировать поток: java.io.FileNotFoundException:/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170217-WA0001.jpg: open failed: Но у меня есть эта строка в манифесте: user2199630

+0

вы используете уровень API> = 23? –

Смежные вопросы