No se puede obtener la imagen de la cámara con el selector de intenciones en 4.2.2 AVD

Estoy trabajando en una parte de mi aplicación que permite al usuario seleccionar una imagen desde la cámara o desde la galería, usando un selector de intenciones.

Está funcionando bien en mi teléfono 2.2.1 android, pero cuando lo compile en un AVD 4.2.2 devuelve un error de puntero nulo cuando uso la cámara,

public void onClick(View View) { Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null); galleryIntent.setType("image/*"); galleryIntent.setAction(Intent.ACTION_GET_CONTENT); galleryIntent.addCategory(Intent.CATEGORY_OPENABLE); Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); Intent chooser = new Intent(Intent.ACTION_CHOOSER); chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent); chooser.putExtra(Intent.EXTRA_TITLE, "Chooser"); Intent[] intentArray = {cameraIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); startActivityForResult(chooser,REQUEST_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) { try { if (bitmap != null) { bitmap.recycle(); } InputStream stream = getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(stream); stream.close(); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } super.onActivityResult(requestCode, resultCode, data); } } 

Este es el error que obtengo:
05-05 05: 03: 31.730: E / AndroidRuntime (820): Causado por: java.lang.NullPointerException

Y dijo que el problema está en esta línea:

 InputStream stream = getContentResolver().openInputStream(data.getData()); 

¿Qué estoy haciendo mal?

Actualizado:

Ahora funciona y aquí está la solución:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) { if(data.getData()!=null) { try { if (bitmap != null) { bitmap.recycle(); } InputStream stream = getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(stream); stream.close(); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { bitmap=(Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(bitmap); } super.onActivityResult(requestCode, resultCode, data); } } 

¡Gracias por tu ayuda!

Hola tengo un intento con este código.

El siguiente código es para el botón de cámara haga clic en obras:

 imgviewCamera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //define the file-name to save photo taken by Camera activity String fileName = "new-photo-name.jpg"; //create parameters for Intent with filename ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, fileName); values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera"); //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState) imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); //create new Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, PICK_Camera_IMAGE); } }); OnActivityresult code will be like below protected void onActivityResult(int requestCode, int resultCode, Intent data) { Uri selectedImageUri = null; String filePath = null; switch (requestCode) { case PICK_Camera_IMAGE: if (resultCode == RESULT_OK) { //use imageUri here to access the image selectedImageUri = imageUri; } else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show(); } break; } if(selectedImageUri != null){ try { // OI FILE Manager String filemanagerstring = selectedImageUri.getPath(); // MEDIA GALLERY String selectedImagePath = getPath(selectedImageUri); if (selectedImagePath != null) { filePath = selectedImagePath; } else if (filemanagerstring != null) { filePath = filemanagerstring; } else { Toast.makeText(getApplicationContext(), "Unknown path", Toast.LENGTH_LONG).show(); Log.e("Bitmap", "Unknown path"); } if (filePath != null) { Toast.makeText(getApplicationContext(), " path" + filePath, Toast.LENGTH_LONG).show(); Intent i = new Intent(getApplicationContext(), EditorActivity.class); // passing array index i.putExtra("id", filePath); startActivity(i); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } } public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); if (cursor != null) { // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } else return null; } 

No se olvide de agregar permiso de cámara en el archivo de manifiesto.
Espero que esto te ayudará.

  • El concepto de una intención en Android?
  • Android: Iniciar una actividad para una aplicación de terceros diferente
  • ¿Debo usar vnd.youtube:videoID?
  • Iniciar el nuevo marcador de Hangouts desde un intento
  • ¿Cómo puedo hacer que mi aplicación aparezca en el selector de intenciones sólo para determinadas URL?
  • ¿Por qué estoy recibiendo el error "MyActivity no es una clase inclusiva?"
  • Android: Intenciones implícitas contra los receptores de difusión
  • ¿Cómo excluir una aplicación específica de ACTION_VIEW?
  • El envío por correo electrónico de texto con formato HTML solo funciona con el cliente de Gmail
  • ¿Cómo podemos utilizar startActivityforResult () para la intención de correo electrónico?
  • Android: android.intent.action.BOOT_COMPLETED en ICS y Gingerbread
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.