打开失败:EACCES(权限被拒绝)并尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()”

Posted

技术标签:

【中文标题】打开失败:EACCES(权限被拒绝)并尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()”【英文标题】:open failed: EACCES (Permission denied) and Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference 【发布时间】:2020-10-19 03:04:28 【问题描述】:

我一直在堆栈中搜索此类问题,但找不到。所以我需要大家的帮助,谢谢。

我一直在使用库 com.blikoon.qrcodescanner 来扫描二维码。在我尝试将库和我的应用程序迁移到 androidx 之前,一切都很好。迁移后,错误如下所示。

    2020-06-29 08:57:52.663 2306-2700/com.asset.assettag E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Screenshots Screenshot_2020-06-29-08-21-34-661_com.android.printspooler.jpg: open failed: EACCES (Permission denied)
2020-06-29 08:57:52.664 2306-2700/com.asset.assettag E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
    Process: com.asset.assettag, PID: 2306
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
        at com.blikoon.qrcodescanner.decode.DecodeImageThread.run(DecodeImageThread.java:29)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
2020-06-29 08:57:52.663 2306-2700/com.asset.assettag E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Screenshots/Screenshot_2020-06-29-08-21-34-661_com.android.printspooler.jpg: open failed: EACCES (Permission denied)

这是我的代码

public class ScanActivity extends AppCompatActivity 
    private Button button;
    private static final int REQUEST_CODE_QR_SCAN = 101;
    private final String LOGTAG = "QRCScanner-ScanActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        button = (Button) findViewById(R.id.button_start_scan);
        button.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                //Start the qr scan activity
                Intent i = new Intent(ScanActivity.this,QrCodeActivity.class);
                startActivityForResult( i,REQUEST_CODE_QR_SCAN);
            
        );

    


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) 
            Log.d(LOGTAG, "COULD NOT GET A GOOD RESULT.");
            if (data == null)
                return;
            //Getting the passed result
            String result = data.getStringExtra("com.blikoon.qrcodescanner.error_decoding_image");
            if (result != null) 
                AlertDialog alertDialog = new AlertDialog.Builder(ScanActivity.this).create();
                alertDialog.setTitle("Scan Error");
                alertDialog.setMessage("QR Code could not be scanned");
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                        new DialogInterface.OnClickListener() 
                            public void onClick(DialogInterface dialog, int which) 
                                dialog.dismiss();
                            
                        );
                alertDialog.show();
            
            return;

        
        if (requestCode == REQUEST_CODE_QR_SCAN) 
            if (data == null)
                return;
            //Getting the passed result
            String result = data.getStringExtra("com.blikoon.qrcodescanner.got_qr_scan_relult");
            Log.d(LOGTAG, "Have scan result in your app activity :" + result);
            AlertDialog alertDialog = new AlertDialog.Builder(ScanActivity.this).create();
            alertDialog.setTitle("Scan result");
            alertDialog.setMessage(result);
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                    new DialogInterface.OnClickListener() 
                        public void onClick(DialogInterface dialog, int which) 
                            dialog.dismiss();
                        
                    );
            alertDialog.show();

        
    

这是库编码和问题定位。

public class DecodeImageThread implements Runnable 
    private static final int MAX_PICTURE_PIXEL = 256;
    private byte[] mData;
    private int mWidth;
    private int mHeight;
    private String mImgPath;
    private DecodeImageCallback mCallback;

    public DecodeImageThread(String imgPath, DecodeImageCallback callback) 
        this.mImgPath = imgPath;
        this.mCallback = callback;
    

    @Override
    public void run() 
        if (null == mData) 
            if (!TextUtils.isEmpty(mImgPath)) 
                Bitmap bitmap = QrUtils.decodeSampledBitmapFromFile(mImgPath, MAX_PICTURE_PIXEL, MAX_PICTURE_PIXEL);
                this.mData = QrUtils.getYUV420sp(bitmap.getWidth(), bitmap.getHeight(), bitmap);
                this.mWidth = bitmap.getWidth();
                this.mHeight = bitmap.getHeight();
            
        

        if (mData == null || mData.length == 0 || mWidth == 0 || mHeight == 0) 
            if (null != mCallback) 
                mCallback.decodeFail(0, "No image data");
            
            return;
        

        final Result result = QrUtils.decodeImage(mData, mWidth, mHeight);

        if (null != mCallback) 
            if (null != result) 
                mCallback.decodeSucceed(result);
             else 
                mCallback.decodeFail(0, "Decode image failed.");
            
        
    

更新库调用解码图像线程的方法

 protected void onActivityResult(int requestCode, int resultCode, final Intent data) 
        如果(结果代码!= RESULT_OK)
            返回;
        
        开关(请求代码)
            案例请求图片:
                结束();
                休息;
            案例 REQUEST_SYSTEM_PICTURE:
                uri uri = data.getData();
                字符串 imgPath = getPathFromUri(uri);
                if (imgPath!=null && !TextUtils.isEmpty(imgPath) &&null != mQrCodeExecutor)
                
                    mQrCodeExecutor.execute(new DecodeImageThread(imgPath, mDecodeImageCallback));
                
                休息;
        
    

非常感谢您的帮助。

【问题讨论】:

评论不用于扩展讨论;这个对话是moved to chat。 【参考方案1】:

我遇到了类似的问题并用它来解决它: https://***.com/a/40623623/13826785

希望这会有所帮助!

【讨论】:

谢谢先生,但是那个解决方案我应该在库中编辑它吗? 是的,在发生错误的库中进行编辑。您可能还需要为高度这样做。 我认为您可能必须将整个库方法替换为链接中发布的方法

以上是关于打开失败:EACCES(权限被拒绝)并尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()”的主要内容,如果未能解决你的问题,请参考以下文章

打开失败:EACCES(权限被拒绝)ANDROID 11

打开失败:Android 中的 EACCES(权限被拒绝)

java.io.filenotfoundexception 在设备上打开失败的 eacces(权限被拒绝)

java.io.FileNotFoundException:/storage/emulated/0/New file.txt:打开失败:EACCES(权限被拒绝)

Android 11 打开失败:EACCES(权限被拒绝)

错误消息“java.net.SocketException:套接字失败:EACCES(权限被拒绝)”