java - BitmapFactory.decodeFile 返回 null

标签 java android android-bitmap

我遇到了几天的问题,但找不到解决方案。我已经在互联网上查找了几个小时,但找不到任何适合我的答案。 我正在制作一个应用程序,我必须将图片发送到网络服务。我正在使用 Fishbun 访问手机图库并选择一些图片。当图片被选取时,我使用 Bitmapfactory.decodeFile() 和 Fishbun 返回的路径在屏幕上显示图片。效果很好。此时的路径是:

/storage/emulated/0/DCIM/Camera/IMG_20160321_044346.jpg

然后,我将路径保存在 ArrayList 中,该列表将发送到 Web 服务。 当我在 ArrayList 的路径上使用 BitmapFactory.decodeFile() 时,它返回 null。路径和第一次一模一样,但是好像两次都无法解码。
如果我重新启动应用程序并在 Fishbun 中取回与第一次相同的图片,decodeFile() 也会返回 null。这是代码......
Fishbun Activity 结果:

protected void onActivityResult(int requestCode, int resultCode,
                                Intent imageData) {
    super.onActivityResult(requestCode, resultCode, imageData);

    switch (requestCode) {
        case Define.ALBUM_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                path = imageData.getStringArrayListExtra(Define.INTENT_PATH);
                int i = 0;
                int error=0;
                while(i<path.size()){
                    Bitmap bmp = BitmapFactory.decodeFile(path.get(i));
                    if(bmp.getWidth()<bmp.getHeight()) // bmp is not null the first time, null the second time
                    {
                        error++;
                        path.remove(i);
                        i--;
                    }
                    i++;
                }
                if(error>=1){
                    Toast.makeText(newStep7.this,R.string.rerrors, Toast.LENGTH_LONG).show();
                }
                if(path.size()>0) {
                    mainAdapter.changePath(path); //display the picture on the screen without changing the path, the method name is kinda wrong
                }
                break;
            }
    }
}

这是发送我的请求之前的decodeFile():

    String imgPath = rep.getImgList().get(0);
    File file = new File(imgPath);
    OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
    Log.d("startedfromthebottom", file.getAbsolutePath()); //show the same path as in the activityresult above
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //bitmap is always null
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,os);

你能帮我找出为什么当我对同一个文件解码两次时我的位图为空吗?

编辑: 我在 Android 设备监视器中找到了该图像文件。图片大小为0,Outputstream之后就不能再使用了

更新:应用 bwt 答案后代码几乎没有变化:

    String imgPath = rep.getImgList().get(0);
    File file = new File(imgPath);
    AtomicFile atomicFile =
            new AtomicFile(file);
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = atomicFile.startWrite();
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        Log.d("showmethebitmap", bitmap.toString()); //Error: bitmap is null !
        oos = new ObjectOutputStream(fos);
        bitmap.compress(Bitmap.CompressFormat.JPEG,0,oos);
        oos.writeObject(bitmap);
        oos.flush();
        atomicFile.finishWrite(fos);
        ...
    } catch (IOException e) {
        atomicFile.failWrite(fos);
        throw e;
    } finally {
        if (oos != null) oos.close();
    }

最佳答案

我知道问题已经得到解答,但是如果有人仍然得到 null,请确保应用程序有权读取用户的文件

关于java - BitmapFactory.decodeFile 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36126806/

相关文章:

java - JPanel 问题 - 在正常外观上重新绘制调整大小

java - LibGDX:仅在特定区域内显示 Sprite

android - 完成其他 Activity 之前的所有 Activity

java - 除非我等待接受照片,否则 BitmapFactory.decodeFile 为空

java - 如何从 JVM 卸载库 (DLL)

java - 将 RestTemplate 交换转换为 okHttpClient 调用

java - AWS Elastic Beanstalk 中的 JDBC 连接字符串

android - viewPager.getChildCount() 在 onActivityResult 之后为零

java - PdfBox renderImageWithDpi 中的形状点不正确

android - 清楚地了解矩阵计算