Android:decodeFile对于内部存储中的文件始终返回null

| 我有一个文件本地保存到应用程序的专用存储中。我已经验证了它的存在,但是每当我调用
BitmapFactory.decodeFile
时,它总是返回
null
。 如果我将文件另存为资源并使用
ImageView.setImageResource
,它总是可以正常显示。 问题是什么? 这是代码段:
filename = \"test.png\";

if (doesFileExist(filename))
    Bitmap bMap = BitmapFactory.decodeFile(filename);
我也尝试过:
Bitmap bMap = BitmapFactory.decodeFile(getFilesDir().getPath()
                    + filename);
    
已邀请:
这个问题已经被回答过了,例如这里: 即使图像存在,BitmapFactory.decodeFile返回null 这正是我所需要的:
String fname=new File(getFilesDir(), \"test.png\").getAbsolutePath();
    

bab

民间,存储在应用程序资源中的文件应以特殊方式引用。例如。如果文件位于资产中并命名为\“ myfile.png \”,则必须引用为:
String uriString=\"file:///android_asset/myfile.png\";
Uri uri=Uri.parse(uriString);
    
代替使用BitmapFactory.decodeFile,请尝试使用InputStream:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    if(resultCode == RESULT_OK){          
        Uri selectedImage = imageReturnedIntent.getData();
        InputStream imageStream = getContentResolver().openInputStream(selectedImage);
        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
    
BitmapFactory.decodeFile
期望不带方案的文件路径。即开头没有
file://
。 如果您要处理Uri,请不要仅将它ri10ѭ,而是要在其上调用call11ѭ并将其传递给方法。     
您可以尝试fileList()吗?它   返回一个命名为   与此相关的私人文件   上下文的应用程序包。     
对我来说,我从本地保存的URL中获取图像,例如\“ file:/// storage / emulated / 0 / .... \”(我曾使用Phonegap插件来捕获图像。该插件为我提供了图像路径,需要在本机代码中使用) 这是为我工作的代码片段。
String captured_image_info = \"file:///storage/emulated/0/Android/data/com.testapp/cache/1493809796526.jpg\"
Uri uri=Uri.parse(captured_image_info);
largeLog(\"uri\", \"\" + uri);

InputStream imageStream = getContentResolver().openInputStream(uri);

Bitmap bm = BitmapFactory.decodeStream(imageStream);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object

byte[] decodedBytes = baos.toByteArray();

Bitmap img_captured_image = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
    

要回复问题请先登录注册