ver telefono tarjeta sdcard samsung puedo porque pasar mover memoria los interno interna externa convertir como cambiar archivos aplicaciones almacenamiento ala android android-sdcard android-image

android - telefono - Al leer un archivo de imagen en un mapa de bits desde una tarjeta SD, ¿por qué recibo una NullPointerException?



file sdcard (4)

¿Cómo puedo leer un archivo de imagen en mapa de bits desde sdcard?

_path = Environment.getExternalStorageDirectory().getAbsolutePath(); System.out.println("pathhhhhhhhhhhhhhhhhhhh1111111112222222 " + _path); _path= _path + "/" + "flower2.jpg"; System.out.println("pathhhhhhhhhhhhhhhhhhhh111111111 " + _path); Bitmap bitmap = BitmapFactory.decodeFile(_path, options );

Obtengo una NullPointerException para bitmap. Significa que el mapa de bits es nulo. Pero tengo un archivo de imagen ".jpg" almacenado en sdcard como "flower2.jpg". ¿Cuál es el problema?


Escribí el siguiente código para convertir una imagen de sdcard en una cadena codificada en Base64 para enviarla como un objeto JSON. Y funciona genial:

String filepath = "/sdcard/temp.png"; File imagefile = new File(filepath); FileInputStream fis = null; try { fis = new FileInputStream(imagefile); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap bm = BitmapFactory.decodeStream(fis); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 100 , baos); byte[] b = baos.toByteArray(); encImage = Base64.encodeToString(b, Base64.DEFAULT);


Funciona:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);


La API de MediaStore probablemente está tirando el canal alfa (es decir, descodificando a RGB565). Si tiene una ruta de archivo, simplemente use BitmapFactory directamente, pero dígale que use un formato que preserve alfa:

BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options); selected_photo.setImageBitmap(bitmap);

o

http://mihaifonoage.blogspot.com/2009/09/displaying-images-from-sd-card-in.html


Prueba este código:

Bitmap bitmap = null; File f = new File(_path); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; try { bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options); } catch (FileNotFoundException e) { e.printStackTrace(); } image.setImageBitmap(bitmap);