-1

I would like to send an email where there is a QR code attach in it. I try to save the QR code which is in Bitmap to the storage, but it always comes out with NullPointerException, no such file or directory is found. Can anyone help? Thanks a lot.

Below are my error message and my code

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();
                }
Saveen
  • 4,120
  • 14
  • 38
  • 41
Jerry ong
  • 13
  • 3

1 Answers1

0

It looks like problem with permission to Storage, i have the same problem recenly. You can check it from your makeDirs() ;

boolean bol = myDir.mkdirs();

If it false it's a permission problem, there are many topics for that problem, for example Storage permission

Stanislav Batura
  • 420
  • 4
  • 11
  • but i do declare permission in my manifest file, which is – Jerry ong Aug 10 '18 at 13:17
  • Read carefully it permission work only on old devices now you have to declare it in your code, in link what i share you can find more information. And for test you may give you app permission on storage throgh the Settings/Apps&Notifications/YourApp/Permissions in you phone or emulator settings. – Stanislav Batura Aug 10 '18 at 14:19
  • 1
    Thanks i forget to provide permission on my device, thanks for the reminding – Jerry ong Aug 31 '18 at 16:37