Im creating a application where a user can register and generate a qr code, after that they can save and share said generated QR image in their preferred medium(i.e. email, telegram, messenger, etc). Now I was able to save the QR image but everytime I try to share it this appear "Couldn't Attach File" message when im trying to send it thru gmail or cannot send image in other media. My expected output is that the image can be attached to the preferred medium of the user.
I have tried adding these in my AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and this but im not really sure what's this used for.
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>
and this is my path.xml file.
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path
name="shared_images"
path="images/"/>
</paths>
Lastly this is my java code for saving and sharing QR image
//Share function
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
BitmapDrawable drawable = (BitmapDrawable)qr_image.getDrawable();
Bitmap bitmap2 = drawable.getBitmap();
FileOutputStream fileOutputStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File Directory = new File(sdCard.getAbsoluteFile() + "/Download");
Directory.mkdir();
String filename = String.format("%d.jpg", System.currentTimeMillis());
File outfile = new File(Directory, filename);
File file = new File(getExternalCacheDir() + "/" + getResources().getString(R.string.app_name) + ".jpeg");
Intent shareInt;
try {
//save
fileOutputStream = new FileOutputStream(outfile);
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(outfile));
sendBroadcast(intent);
Toast.makeText(RegisterStudent.this, "QR Image Saved", Toast.LENGTH_SHORT).show();
//share
shareInt = new Intent(Intent.ACTION_SEND);
shareInt.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareInt.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
shareInt.setType("image/jpeg");
shareInt.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareInt.putExtra(Intent.EXTRA_SUBJECT, "QR Code");
shareInt.putExtra(Intent.EXTRA_TEXT, "Please don't share your QR Code to anyone.");
shareInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
throw new RuntimeException(e);
}
startActivity(Intent.createChooser(shareInt, "Share image via"));
dialog.dismiss();
}
});
dialog.show();
}
Sorry this is my second time using stackoverflow if you have any questions or request that may help me fix my code please comment! Thank you!