FileNotFound异常,我无法将我的位图保存到存储中

时间:2018-08-10 11:28:43

标签: android bitmap qr-code

我想发送一封带有QR码的电子邮件。我尝试将位图中的QR码保存到存储中,但它总是随NullPointerException一起出现,找不到此类文件或目录。有人可以帮忙吗?非常感谢。

下面是我的错误消息和代码

enter image description here

MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                try{
                    BitMatrix bitMatrix = multiFormatWriter.encode(key, BarcodeFormat.QR_CODE,200,200);
                    BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                    Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                    //File file = BitmapSaver.saveImageToExternalStorage(AddReservationDetailActivity.this,bitmap);
                    File myDir = new File(Environment.getExternalStorageDirectory()+"/req_images");
                    myDir.mkdirs();
                    DateFormat format = new SimpleDateFormat("yyyy_MM_dd_H_mm_ss", Locale.getDefault());
                    Date curDate = new Date();
                    String displayDate = format.format(curDate);
                    String fname = displayDate+ "_Image-Report.jpg";
                    File file = new File(myDir,fname);
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[]{email});
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT,"This QR code is for your check In purpose");
                    emailIntent.putExtra(Intent.EXTRA_TEXT," ");
                    emailIntent.setType("image/*");
                    try{
                        FileOutputStream outputStream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
                        outputStream.close();
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }
                    emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
                    startActivity(Intent.createChooser(emailIntent,"Send the email in: "));
                }catch (WriterException e){
                    e.printStackTrace();
                }

1 个答案:

答案 0 :(得分:0)

似乎有存储许可问题,我最近也遇到同样的问题。您可以从makeDirs()进行检查;

boolean bol = myDir.mkdirs();

如果为假,则为权限问题,该问题有很多主题,例如Storage permission