2015-11-12 4 views
0

Проблема: когда я беру фото с камеры, а камера имеет какую-либо ориентацию, чем активность, я получаю исключение из null-указателя из активности в BitmapFactory.decodeFile. (например, активность имеет вертикальную ориентацию, а камера имеет горизонтальное) Мой код:(Android) -> NPE -> BitmapFactory: Невозможно декодировать поток

private void dispatchTakePictureIntent() throws IOException { 

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      System.out.println("file1986 - can not"); 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
        Uri.fromFile(photoFile)); 
      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
     } 
    } 
} 

затем

private File createImageFile() throws IOException { 
    // Create an image file name 
    String imageFileName = "temp_v_" + getIntent().getStringExtra("idVehicle") + "_p_" + getIntent().getStringExtra("idPlace"); 
    File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = image.getAbsolutePath(); 
    return image; 
} 

затем

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) { 
      Bitmap img = BitmapFactory.decodeFile(mCurrentPhotoPath,options); 
      resizeImage(img,mCurrentPhotoPath); 
      foto.setImageBitmap(img); 
    } 
} 



public static void resizeImage(Bitmap source, String file) { 
    Bitmap tempImage = Bitmap.createScaledBitmap(source, source.getWidth()/5, source.getHeight()/5, false); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    tempImage.compress(Bitmap.CompressFormat.JPEG, 80, bytes); 
    File f = new File(file); 
    try { 
     f.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

Журналы ошибок:

11-13 01:16:34.771 31144-31144/mobile.observer.marketirs.com.observermobile E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException 
11-13 01:16:34.773 31144-31144/mobile.observer.marketirs.com.observermobile E/test: Exception 
11-13 01:16:34.783 31144-31144/mobile.observer.marketirs.com.observermobile E/AndroidRuntime: FATAL EXCEPTION: main 
11-13 01:16:34.783 31144-31144/mobile.observer.marketirs.com.observermobile E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {mobile.observer.marketirs.com.observermobile/mobile.observer.marketirs.com.observermobile.PhotoActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/temp_v_112_p_12711790753674.jpg typ=image/jpeg (has extras) }} to activity {mobile.observer.marketirs.com.observermobile/mobile.observer.marketirs.com.observermobile.PhotoActivity}: java.lang.NullPointerException 
+0

я думаю, что это связь с OutOfMemory ERR, но я не вижу в журналах. –

ответ

0

Я решил эту задачу. Проблема заключалась в том, что действие было перезапущено с измененной ориентацией. И все envs будут иметь значение null или default.

это сообщение stackoverflow

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