Upto Android 9, i am using below code and it is working:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
OutputStream fOut;
Uri outputFileUri;
try {
int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String timestamp = tsTemp.toString();
File root = new File(Environment.getExternalStorageDirectory() + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, timestamp + ".jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("*/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, "android.resource://" + getPackageName() + "/drawable/ic_launcher");
sharingIntent.putExtra(Intent.EXTRA_STREAM, outputFileUri);
String shareBody = "Download our app now";
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Easy Mobile");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share"));
} catch (Exception e) {
e.printStackTrace();
}
From Android 10 or 11 onwards, this code is not working. I took runtime permissions also, and accepted those. But I see the below errors in logcat:
->Primary directory null not allowed for content://media/external_primary/file; allowed directories are [Download, Documents]
->java.io.FileNotFoundException: /storage/emulated/0/19691213181944618.jpg: open failed: EPERM (Operation not permitted).
Can anyone help me with proper solution? (java)
Related
I am tring to share an image which i fetch from Json. After a number of attempts i finally concluded with this code. It works well till i open whatsapp and select the contact i want to share, the image doesnt load from there and when i click on submit, it says sharing failed.
Drawable drawable1 = imageViewPreview.getDrawable();
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.id.image_preview);
File file = new File(SlideshowDialogFragment.this.getCacheDir(), String.valueOf(largeIcon + ".png"));
// FileOutputStream fOut = new FileOutputStream(file);
// largeIcon.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
file.setReadable(true, false);
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(intent);
if i remove the comments
FileOutputStream fOut = new FileOutputStream(file);
will show error message and i have to add try/catch then, the app crashes saying Nullpoint exception
Try this code:
Bitmap bitmap = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "photo.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/photo.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
I need to share an image (.png format) with transparent background, through sent_action intent.
I searched a lot and tried a lot of samples but couldn't find the solution.
The is this method in witch the image will get shared directly from the resources, but for some reason from some point it has stop working.
Uri url = Uri.parse("android.resource://"
+ getPackageName() + "/" + R.drawable.ic_launcher);
Intent share_intent = new Intent();
share_intent.setAction(Intent.ACTION_SEND);
share_intent.setType("image/png");
share_intent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(url.toString())));
startActivity(Intent.createChooser(share_intent, "choose app"));
and there is this function which works fine, the problem is it adds a black background to the image.
private void share3()
{
Bitmap bitmap;
OutputStream output;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/Gallery/");
dir.mkdirs();
File file = new File(dir, "ic_launcher" + ".png");
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
output = new FileOutputStream(file);
bitmap. compress(Bitmap.CompressFormat.PNG/*Bitmap.CompressFormat.PNG*/, 0, output);
output.flush();
output.close();
Uri uri = Uri.fromFile(file);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "choose app"));
} catch (Exception e) {
e.printStackTrace();
}
}
I need to share the image from "raw" folder in the resources and share it without background. What should I do?
I found the problem, It is telegram itself. Telegram adds a black background to .png images you share.
Try this ..its work for me to share image from drawable folder.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/LatestShare.png";
OutputStream out = null;
File file = new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path = file.getPath();
Uri bmpUri = Uri.parse("file://" + path);
Intent shareIntent = new Intent();
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "Share with"));
I am writing an app to share image.
I wrote the code but getting problems.
What I am doing
1: Saving the image temporarily in Internal Stoarge
2: Passing the URI to share the image.
Image is successfully saved in internal storage but not getting share on whatsapp.
When I share it on whatsapp, Whatsapp get opens, I select recipient, whatsapp processes it and says "Retry".
Code:
public void shareImage(View V)
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.download);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDownloadedImage.jpg");
try
{
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
Toast.makeText(this, "Some error in Writing"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Uri downloadLocation=Uri.fromFile(file);
share.putExtra(Intent.EXTRA_STREAM, downloadLocation);
startActivity(Intent.createChooser(share, "Share Image"));
}
Image succesfully get saved on internal storage but not getting shared on whatsapp.
The screenshot is below. We can see images are not shared successfully.
Use below code to fetch image and make uri:
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
Now pass the bmpUri in:
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
This should make your image share.
I'm creating an ACTION_SEND intent in android and attaching an image file with the following code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, "");
intent.putExtra(Intent.EXTRA_SUBJECT, "Receipt From: XXX");
intent.putExtra(Intent.EXTRA_TEXT, message);
byte[] b = Base64.decode(signature, Base64.DEFAULT); //Where signature is a base64 encoded string
final Bitmap mBitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
String path = Images.Media.insertImage(getContentResolver(), mBitmap, "signature", null);
Uri sigURI = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, sigURI);
startActivity(Intent.createChooser(intent, "Send Email"));
This works, the image is attached to the email. However I'm having trouble deleting the image afterwords. The images are getting stored in the DCIM\Camera folder with a large number as the file name.
I've tried
File tempFile = File(getRealPathFromURI(sigURI)); //a subroutine which gives me the full path
tempFile.delete();
tempFile.delete returns true, however the file is still there.
One odd thing I noticed is that the image saved is of file size 0 and appears empty both before and after I try to delete.
How do I properly delete this image after sending it with the email? Or is there an alternative way of attaching the image without saving it?
Also, not the main question here but if you could include how to change the name of the image/attachment from 1375729812685.jpg (or what ever the number may be) to something else, I'de appreciate it.
As a last note, I've been testing on an HTC Evo if it makes any difference.
I have found a solution for anyone who is interested.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{});
intent.putExtra(android.content.Intent.EXTRA_CC, new String[ {"test#test.net"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Receipt From: " + receipt[1]);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File albumF = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Receipt");
if (albumF != null) {
if (! albumF.mkdirs()) {
if (! albumF.exists()){
Log.d("CameraSample", "failed to create directory");
return;
}
}
}
File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
byte[] b = Base64.decode(sig, Base64.DEFAULT);
if (b.length > 0) {
Bitmap mBitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
FileOutputStream ostream = new FileOutputStream(imageF);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.flush();
ostream.close();
Uri fileUri = Uri.fromFile(imageF);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
}
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Send Email Using"));
My problem is whenever I save the bitmap. I saves one in the folder I want(MyFolder) and one in the DCIM/Camera. I don't see any code that makes it save int he DCIM directory?
Here are my codes...
case R.id.menu_save:
try {
String path = Environment.getExternalStorageDirectory()
.toString();
File myNewFolder = new File(path + "/MyFolder");
myNewFolder.mkdirs();
OutputStream fOut = null;
File file = new File(path, "/MyFolder/HK" + filename + ".jpg");
fOut = new FileOutputStream(file);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), file.getName());
Toast.makeText(getApplicationContext(),
filename + "Has been saved!", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(
getApplicationContext(),
"Problem to Save the File", Toast.LENGTH_LONG).show();
}
break;
As in the thread #Dixit linked, you can specify the file path URI with
File fileDir = new File(Environment.getExternalStorageDirectory() +
"/saved_images");
fileDir.mkdirs();
File file = new File(fileDir, "image.jpg");
Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
This way the camera will save the image to the specified path instead of the DCIM folder.
EDIT: You have to create the folder on the sdcard beforehand, maybe that's the problem. Otherwise, this should work.