Android – ImageView no muestra la foto tomada con la cámara

Por favor, tenga paciencia conmigo … he estado buscando DAYS por un trabajo, desnudo-huesos fragmento de código que inicia la actividad de la cámara, toma una foto, y lo coloca en un simple ImageView>. <El código publicado por debajo de los incendios Hasta la actividad y toma la foto bien, pero la imagen no se muestra en el ImageView! ¿Qué es lo que falta? Unesdoc.unesco.org

public class MainActivity extends Activity { private static final int PICK_IMAGE = 0; private static final int PICK_IMAGE_FROM_GALLERY = 1; private Button mBtnCamera, mBtnGallery, mBtnCancel; private ImageView mImageView; private Uri mURI; private String mPhotoPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView) findViewById(R.id.imgDisplayImage); mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera); mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery); mBtnCancel = (Button) findViewById(R.id.btnCancel); mBtnCamera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent camera = new Intent(); camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE); camera.putExtra("crop", "true"); File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg")); camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI); startActivityForResult(camera, PICK_IMAGE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICK_IMAGE) { // Result includes a Bitmap thumbnail? if (data != null) { if (data.hasExtra("data")) { //Bitmap thumbnail = data.getParcelableExtra("data"); Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); mImageView.setImageBitmap(thumbnail); } } // If there is no thumbnail data, the image will have been stored in target output URI. else { Cursor cursor = getContentResolver().query( Media.EXTERNAL_CONTENT_URI, new String[] { Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION }, Media.DATE_ADDED, null, "date_added ASC" ); if (cursor != null && cursor.moveToFirst()) { do { mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA))); mPhotoPath = mURI.toString(); } while (cursor.moveToNext()); cursor.close(); } // Resize full image to fit out in image view. int width = mImageView.getWidth(); int height = mImageView.getHeight(); BitmapFactory.Options factoryOptions = new BitmapFactory.Options(); factoryOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions); int imageWidth = factoryOptions.outWidth; int imageHeight = factoryOptions.outHeight; // Determine how much to scale down the image int scaleFactor = Math.min( imageWidth/width, imageHeight/height ); // Decode the image file into a Bitmap sized to fill view factoryOptions.inJustDecodeBounds = false; factoryOptions.inSampleSize = scaleFactor; factoryOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions); mImageView.setImageBitmap(bitmap); } } } } 

Tuve el mismo problema en algunos dispositivos de samsung android Luego implementé la lógica para obtener el camino de la foto capturada.

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICK_IMAGE) { Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE); Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC"); if(cursor != null && cursor.moveToFirst()) { do { uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA))); photoPath = uri.toString(); }while(cursor.moveToNext()); cursor.close(); } if(photoPath != null) { Bitmap bitmap = BitmapFactory.decodeFile(photoPath); ///Do Implement your logic whatever you want. mImageView.setImageBitmap(bitmap); } } } 

Probado y probado para trabajar en un teléfono Galaxy S3. Agradecemos a los TGM por su ayuda.

 public class MainActivity extends Activity { private static final int PICK_IMAGE = 0; private static final int PICK_IMAGE_FROM_GALLERY = 1; private Button mBtnCamera, mBtnGallery, mBtnCancel; private ImageView mImageView; private Uri mURI; private String mPhotoPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView) findViewById(R.id.imgDisplayImage); mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera); mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery); mBtnCancel = (Button) findViewById(R.id.btnCancel); mBtnCamera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent camera = new Intent(); camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE); camera.putExtra("crop", "true"); File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg")); camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI); startActivityForResult(camera, PICK_IMAGE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == PICK_IMAGE) { Cursor cursor = getContentResolver().query( Media.EXTERNAL_CONTENT_URI, new String[] { Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION }, Media.DATE_ADDED, null, "date_added ASC" ); if (cursor != null && cursor.moveToFirst()) { do { mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA))); mPhotoPath = mURI.toString(); } while (cursor.moveToNext()); cursor.close(); } if (data != null) { if (data.hasExtra("data")) { Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); mImageView.setImageBitmap(thumbnail); } else { System.out.println("Intent bundle does not have the 'data' Extra"); int width = mImageView.getWidth(); int height = mImageView.getHeight(); BitmapFactory.Options factoryOptions = new BitmapFactory.Options(); factoryOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions); int imageWidth = factoryOptions.outWidth; int imageHeight = factoryOptions.outHeight; // Determine how much to scale down the image int scaleFactor = Math.min( imageWidth/width, imageHeight/height ); // Decode the image file into a Bitmap sized to fill view factoryOptions.inJustDecodeBounds = false; factoryOptions.inSampleSize = scaleFactor; factoryOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions); mImageView.setImageBitmap(bitmap); } } } } else { System.out.println("Picture taking activity NOT returning RESULT_OK"); } } } 

Trate de añadir

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 

(Y un ligero retraso) dentro de la

  if (requestCode == PICK_IMAGE) 

bloquear. Creo que el problema podría ser que su dispositivo no está actualizando el almacén de medios correctamente.

(Por supuesto, la mejor acción sería usar MediaScanner para escanear su archivo )

  • ¿Cuál es la manera más eficiente de cargar mi archivo JPG almacenado en ImageView?
  • MediaStore.Images.Media.getBitmap y fuera de error de memoria
  • Cambiar un icono en una entrada de Android ListView cuando se presiona
  • ¿El android: scaleType = "fitCenter" sólo funciona con los atributos fix Layout_width y Layout_height?
  • Carga de mapas de bits grandes a ImageView en ViewPager - sin memoria
  • Esquinas redondeadas para una vista de imagen en android
  • Android: ImageView getID (); Devolver entero
  • Cómo mostrar la imagen de YUV en Android
  • Pinch Zoom in android para una vista de imagen?
  • No se puede cargar la imagen tomada desde la cámara en un ImageView
  • Efecto ondulado en estado prensado + transparencia en estado normal
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.