gpt4 book ai didi

java - 无法在android中将uri转换为位图

转载 作者:行者123 更新时间:2023-11-29 19:42:54 25 4
gpt4 key购买 nike

我正在尝试将谷歌文档转换为位图,以便我可以在其中执行 OCR。但是我收到了错误:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /document/acc=4;doc=14882: open failed: ENOENT (No such file or directory)

E/ReadFile: Bitmap must be non-null

E/CropTest: Failed to read bitmap

代码:

/**
* Fires an intent to spin up the "file chooser" UI and select an image.
*/
public void performFileSearch(View view) {

// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
intent.addCategory(Intent.CATEGORY_OPENABLE);

// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
// To search for all documents available via installed storage providers,
// it would be "*/*".
intent.setType("application/vnd.google-apps.document");

startActivityForResult(intent, READ_REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData){

if(requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK){
Uri uri = null;
if(resultData != null){
uri = resultData.getData();
Log.i(TAG, "Uri" + uri.toString());

Toast.makeText(MainActivity.this, "Uri:" + uri.toString(), Toast.LENGTH_LONG).show();
IMGS_PATH = Environment.getExternalStorageDirectory().toString()+ "/TesseractSample/imgs";
prepareDirectory(IMGS_PATH);
prepareTesseract();
startOCR(uri);
}
}
}

//Function that begins the OCR functionality.
private void startOCR(Uri imgUri) {
try {

Log.e(TAG, "Inside the startOCR function");
BitmapFactory.Options options = new BitmapFactory.Options();
// 1 - means max size. 4 - means maxsize/4 size. Don't use value <4, because you need more memory in the heap to store your data.
options.inSampleSize = 4;
// FileOutputStream outStream = new FileOutputStream(String.valueOf(imgUri));
Bitmap bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgUri);
// bm.compress(Bitmap.CompressFormat.PNG,100,outStream);

Bitmap bitmap = BitmapFactory.decodeFile(imgUri.getPath());
// bitmap = toGrayscale(bitmap);

//The result variable will hold whatever is returned from "extractText" function.
result = extractText(bm);

//Creating the intent to go to the CropTest
Intent intentToCropTest = new Intent(MainActivity.this, CropTest.class);
intentToCropTest.putExtra("result",result);
startActivity(intentToCropTest);

//Setting the string result to the content of the TextView.
// textView.setText(result);

} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}

最佳答案

您正在尝试将 Android Uri 视为文件路径。不要那样做。取而代之的是检索一个 ContentResolver 实例并使用它来将 Uri 转换为流:

AssetFileDescriptor fd = context.getContentResolver()
.openAssetFileDescriptor(uri, "r");
InputStream is = fd.createInputStream();

如果不支持 AssetFileDescriptor(您得到 null 或发生异常),请尝试更直接的途径:

InputStream is = context.getContentResolver().openInputStream();

还有另一种 super 强大的内容类型感知方法,它存在了很长时间,但是是re-discovered by Google's own developers around the time of Android N release .它在 ContentProvider 端需要更多的基础设施(旧版本的 Google 服务可能不受支持):

ContentResolver r = getContentResolver();

String[] streamTypes = r.getStreamTypes(uri, "*/*");

AssetFileDescriptor descriptor = r.openTypedAssetFileDescriptor(
uri,
streamTypes[0],
null);

InputStream is = descriptor.createInputStream();

然后使用获取的流创建Bitmap:

Bitmap bitmap = BitmapFactory.decodeStream(is);

关于java - 无法在android中将uri转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38312023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com