Hacer referencia a un archivo en la tarjeta SD, Android

I currently trying to carry out the same operation (OCR) on images in android, one option is to use the camera and the other is to load an image from the SD-CARD. The code works when taken from a camera where the code is

Bitmap bitmap = BitmapFactory.decodeFile(_path,options);

where _path is equal to the last image taken using the application ( _path = DATA_PATH + "ocr.jpg"; ) . However when I try to use an image selected from the gallery where _path would equal,

imageCaptureUri = data.getData();
_path = imageCaptureUri.getPath();

The program locks up with the error

Failure deleiving result ResultInfo{who=null, request = 2, result = -1, data=intent {dat=content://media/external/images/media/26 typ=image/jpeg(has extras)}} to activity{com.project.projectActivity}: java.lang.NullPointerException

If anybody has an idea of whats going on I'd like to hear from you !!

preguntado el 09 de marzo de 12 a las 16:03

2 Respuestas

You can get path of the image as..

_path = getPath(imageCaptureUri);

public String getPath(Uri uri) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
column_index = cursor
imagePath = cursor.getString(column_index);

return cursor.getString(column_index);

y entonces

Bitmap bitmap = BitmapFactory.decodeFile(_path,options);

Nota: : If you are cropping image then this method does not work in this case Gallery will generate cropped image on same directory of image which you are cropping.

respondido 09 mar '12, 16:03

The Gallery returns you an Uri to access the image.

Necesitas usar el decodeStream method from BitmapFactory, and for that you need to open an InputStream on the Uri given :

InputStream is = getContentResolver().openInputStream(imageCaptureUri);
Bitmap bitmap = BitmapFactory.decodeStream(is, options);

respondido 09 mar '12, 16:03

just trying this now, getting an error which says decodeSteam(InputStream) cannot be applied to (InputStream,BitmapFactory.Options), Also thanks very much for the reply ! - Tom Selleck

yes, my bad, you need to specify an out Rect desarrollador.android.com/reference/android/graphics/… - njzk2

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.