简体   繁体   English

引用Android SD卡上的文件

[英]Referencing a file on SD-Card, 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. 我目前正在尝试对android中的图像执行相同的操作(OCR),一种选择是使用相机,另一种选择是从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"; ) . 其中_path等于使用该应用程序拍摄的最后一张图片( _path = DATA_PATH + "ocr.jpg"; )。 However when I try to use an image selected from the gallery where _path would equal, 但是,当我尝试使用从_path相等的图库中选择的图像时,

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 失败发送结果ResultInfo {who = null,请求= 2,结果= -1,数据=意图{dat = content:// media / external / images / media / 26 typ = image / jpeg(has extras)}}到活动{com.project.projectActivity}:java.lang.NullPointerException

If anybody has an idea of whats going on I'd like to hear from you !! 如果有人对发生的事情有任何想法,我想听听您的消息!

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
    .getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);

return cursor.getString(column_index);
}

and then 接着

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

Note : 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. 注意 :如果要裁剪图像,则此方法在这种情况下不起作用,因此Gallery会在要裁剪的图像的同一目录上生成裁剪的图像。

The Gallery returns you an Uri to access the image. 图库向您返回一个Uri来访问图像。

You need to use the decodeStream method from BitmapFactory, and for that you need to open an InputStream on the Uri given : 您需要使用BitmapFactory中的decodeStream方法,为此,您需要在给定的Uri上打开InputStream:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM