Share Option for PNGs - java

I am trying to implement a share option for png pictures in my app, but I have been getting a TransactionTooLargeException. What I did: I added code to compress my Bitmap, but I still get the exception. Is there something I am doing wrong?
public void sharePicture(MenuItem shareItem) {
MenuItemCompat.getActionProvider(shareItem);
Drawable drawable = itemImage.getDrawable();
Bitmap picture = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.PNG, 100, stream);
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, picture);
startActivity(Intent.createChooser(shareIntent, "Share image using"));
}

Quoting the documentation, EXTRA_STREAM is:
A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
You do not put a Bitmap into EXTRA_STREAM for an image/png ACTION_SEND request. You put a Uri pointing to a PNG into EXTRA_STREAM. Ideally, you do that via FileProvider, though if your targetSdkVersion is below 24, you can get away with Uri.fromFile() for the time being.

Related

Android - Intent Sharing

How to detect from which (any) external app(or packagname) data was shared:
Via sharing an image/data (intent) from another app to my app(activity),
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}

Android: Sharing Bitmap with Intents on other apps

I have a bitmap file that I created using Android Query:
aq = new AQuery(HomeCategoryActivity.this);
aq.ajax(currentUrl,Bitmap.class,0,new AjaxCallback<Bitmap>(){
#Override
public void callback(String url, Bitmap object, AjaxStatus status) {
if(object != null)
{
bmp = object;
}
}
});
bmp is a globally initialized variable and it gets properly saved by the above code and NOT NULL, I checked.
Now I want to share this bitmap on other apps using this:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(bmp));
startActivity(Intent.createChooser(intent, "Share Product via:"));
This won't work since the code is probably wrong. What changes should I make?
I want to share the image on Fb, insta, etc
Save bitmap to external storage and get path of the bitmap image
then pass the Uri.parse(path)to the intent.
for more information refer to this link http://developer.android.com/training/sharing/send.html
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parseUri(path));
startActivity(Intent.createChooser(intent, "Share Product via:"));

How to send image from one activity to another in kitkat android?

In first Activity:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
i.putExtra("image", bytes);
startActivity(i);
In second Activity:
byte[] byteArray = extras.getByteArray("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
if (bmp != null) {
iv_1.setImageBitmap(bmp);
}
This is working for all devices and versions. But it is not working for Kitkat, why?
How to solve the issue in kitkat?
Passing such a huge file through intent is not a good practice. It would slow down the process of launching new activity.
Try to make a static reference of the image and use it in the next activity. As soon as you are done, just make it null
Make a singleton class with a Map<String, Bitmap> which will keep all images you need, and through intent send just their key names.
It is not a good for performance to pass Bitmaps from on activity to another.
Just try to save the bitmap in memory and send "Path" of bitmap to another activity and then just use BitmapFactory.decodeFile(pathName);method in another activity to get bitmap from path.
In your first activity convert imageview to bitmap
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
in second activity
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
Its working on kitkat also.

Share BMP with ShareActionProvider?

In my app I am trying to share a BMP with the ShareActionProvider. It is not working. Am I doing it wrong or do I need to convert it into a PNG (I do not want to deal with files). If so how can I do it? THanks.
Bitmap bmp = qrUtil.create(WIFIQRCODE, pref2);
isCodeGenerated = true;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, bmp);
provider.setShareIntent(intent);
The Intent.EXTRA_STREAM should be a URI pointing to the image file that you want to use; you can't just add a Bitmap there. You should do something like this:
Uri uri = Uri.fromFile(new File(getFilesDir(), "img.jpg"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
This will change depending on the actual location of your image, but that's the general idea.

Android: Share Image intent not working with Facebook?

Hi I have the following code to share an image:
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
Uri uri = Uri.parse(getFilesDir() + File.separator + "myGoal.jpg");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
It works to share the image to Dropbox but if I pick the Facebook option, I get Facebook's status update dialog with no image attached and if I try to update my status with 'Test' it doesn't work. No errors. Just not working.
I know it's not the image because it uploads to my Dropbox properly and I can pull up the image and look at it.
Do I have to attach the image to the intent differently for it to work with Facebook?
Any ideas? I'm debugging on a physical device.
So I figured out the problem.
I was saving the picture to internal storage with getFilesDir() which put the picture into my apps sandbox and made inaccessible to the other apps.
I replaced my code with the following:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/MyApp/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "myPic.png");
FileOutputStream fOut = new FileOutputStream(file);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "My Image");
startActivity(Intent.createChooser(share, "Share Image"));
Works perfectly fine now.
What i have done to post image on facebook is instead of passing it directly i create a function and write it like this :
private void ShareWall(String message) {
Bundle parameters = new Bundle();
// share msg
parameters.putString("message", message);
// shre image which you have define above
parameters.putString("picture", postImage);
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("response: ", response);
if (response == null || response.equals("")) {
response.equals("");
showToast("No Response.");
} else {
showToast("Message has been posted to your walll!.");
}
finish();
} catch (Exception e) {
showToast("Message failed to posdt on wall.");
e.printStackTrace();
finish();
}
}
Hope this help you.
You can still share images (but not text) from your app to Facebook even if you are not using the Facebook SDK.
Just make sure that you use Uri.fromFile instead of Uri.parse and it will work:
DO NOT USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathToFile));
USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(pathToFile)));
Here is a solution without using external file write:
Drawable mDrawable = myImageView1.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image I want to share", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));
In this case, my image comes from an ImageView myImageView.

Categories