I am trying to share image from resources with other apps (like Viber or Facebook).
I used the code below to try and do this, but it is failing:
Bitmap bitmapToShare = BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Drawable.A1);
Java.IO.File pictureStorage = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
Java.IO.File noMedia = new Java.IO.File(pictureStorage, ".nomedia");
if (!noMedia.Exists())
noMedia.Mkdirs();
string imageName = "shared_image" + System.DateTime.Now.ToBinary() + ".Jpeg";
Stream output = Application.Context.OpenFileOutput(imageName,FileCreationMode.WorldReadable);
bitmapToShare.Compress(Bitmap.CompressFormat.Jpeg, 100, output);
output.Flush();
output.Close();
Java.IO.File file = new Java.IO.File(noMedia, imageName);
shareIntent.SetType("image/*");
shareIntent.PutExtra(Intent.ExtraStream, Uri.FromFile(file));
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
StartActivity(Intent.CreateChooser(shareIntent,"Share with"));
For what it's worth, the file is not saved after (output.close;). Why is this failing?
Related
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)
I am trying to insert a file in MediaStore and then share the same file using the uri generated.
For Inserting the file
ImageView image = (ImageView) findViewById(R.id.image);
Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
String fileName = "myFile.jpg";
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bytes);
File enternalStroageDir = Environment.getExternalStorageDirectory();
File file = new File(enternalStroageDir + File.separator + fileName);
FileOutputStream fileOutputStream = null;
try {
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(bytes.toByteArray());
ContentResolver cr = getContentResolver();
String imagePath = file.getAbsolutePath();
String name = file.getName();
String description = "My bitmap created by Android-er";
String savedURL = MediaStore.Images.Media
.insertImage(cr, imagePath, name, description);
Now, the insert is working fine as I checked in the gallery.
Futher, to share the same image, I add the following code.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, savedURL);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "SendText"));
However, when I choose any application that are suggested in the chooser, the sharing of file gets failed.
Any help will be appreciated.
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"));