简体   繁体   English

Android中使用Intent打开文件管理器后出现红框

[英]Red frame after using Intent to open file manager in Android

I am trying to open file manager to import a QR Code image but after using Intent to open file manger and import the QR Code, my app runs with a red frame around the screen.我正在尝试打开文件管理器以导入 QR 码图像,但在使用 Intent 打开文件管理器并导入 QR 码后,我的应用程序在屏幕周围运行时出现红框。 I have googled it and found that, the strict mode should be deactivated and then the device should also be reset.我用谷歌搜索了它,发现应该禁用严格模式,然后也应该重置设备。 But it didn't work at all.但它根本没有用。 My code is below:我的代码如下:

 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    qrList = new ObservableArrayList<>();
    bindAndAttachContentView(R.layout.ui_parkplatz, savedInstanceState);
    setSupportToolbar(binding.toolbar);
    setDrawer(binding.toolbar);

    FloatingActionButton addParkingAccessCode = findViewById(R.id.floating_action_button);
    addParkingAccessCode.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Timber.v("Click add qr code image");
            openFileManagerForQRCode();
        }
    });

    Timber.v("Enter Parkplatz activity");

    binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
    binding.recyclerView.setAdapter(new ParkplatzAdapter(qrList, this));
}


private void openFileManagerForQRCode() {
    //Intent.ACTION_GET_CONTENT
    Intent pickIntent = new Intent(Intent.ACTION_GET_CONTENT);
    pickIntent.setType("image/*");
    //pickIntent.addCategory(Intent.CATEGORY_OPENABLE);
    try{
        this.startActivityForResult(Intent.createChooser(pickIntent, "Select QRCode"), QRCODE_REQUEST);
    }catch (android.content.ActivityNotFoundException e){
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (resultCode == RESULT_OK && requestCode == QRCODE_REQUEST) {
        if (data == null || data.getData() == null) {
            Timber.v("The uri is null, probably the user cancelled the image selection process using the back button.");
            return;
        }

        //SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(SHARED_PREFERENCES_QR_CODE, Context.MODE_PRIVATE);
        //SharedPreferences.Editor editor = sharedPreferences.edit();
        SQLiteDBHelper sqLiteDBHelper = new SQLiteDBHelper(getApplicationContext());

        final Uri uri = data.getData();
        String qrCodeContent = null;

        try {
            InputStream inputStream = getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            if (bitmap == null) {
                Timber.v("URI is not a bitmap " + uri.toString());
                return;
            }

            int width = bitmap.getWidth(), height = bitmap.getHeight();
            int[] pixels = new int[width * height];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            //bitmap.recycle();
            //bitmap = null;
            //RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
            LuminanceSource source = new RGBLuminanceSource(width, height, pixels);
            BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
            //MultiFormatReader reader = new MultiFormatReader();
            Reader reader = new MultiFormatReader();
            Result result = null;

            try {
                result = reader.decode(bBitmap);
                qrCodeContent = result.getText();

                Timber.v("qrCode content "+qrCodeContent);
                Toast.makeText(this, "Saved QRCode", Toast.LENGTH_SHORT).show();
            } catch (NotFoundException e) {
                Timber.v("Decode exception " + e);
            } catch (FormatException e) {
                Timber.v("Decode exception " + e);
            } catch (ChecksumException e) {
                Timber.v("Decode exception " + e);
            }

            inputStream.close();
        } catch (IOException e) {
            Timber.v("Error " + e + ". Can not open file" + uri.toString());
        } finally {
            if (qrCodeContent != null) {
                //editor.putString(random(), qrCodeContent);
                //editor.commit();

                if (sqLiteDBHelper.insertQRCode(qrCodeContent)) {
                    Timber.v("Insert QRCode successful");
                } else {
                    Timber.v("Insert QRCode not successful");
                }
            }
        }
    }else{
        Timber.v("Import QRCode failed");
    }

}

try to analyze the *Stack trace* output, it will give you a better idea !尝试分析*Stack trace* output,它会给你一个更好的主意

also check all the required permissions还要检查所有必需的权限

  • like managing storage files.比如管理存储文件。

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

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