Android: Sharing Bitmap with Intents on other apps - java

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:"));

Related

share image from application folder

I want share image from on click of button
Java code
public void share(View v)
{
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
Uri a=Uri.parse("android.resource://"+getPackageName()+"/"+R.drawable.pic);
Log.i("imageUri",""+imageUri);
share.putExtra(Intent.EXTRA_STREAM,a);
startActivity(Intent.createChooser(share,"Share Image"));
}
by this code is not working , what changes should i do ?
Try this,
First You need to add permission WRITE_EXTERNAL_STORAGE
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Note : for Marshmallow and above version You need Runtime Permission of WRITE_EXTERNAL_STORAGE and Here is good example of Runtime Permission for EXTERNAL_STORAGE
Then use following code to share your Image
Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.pic);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "Title", null);
Uri a= Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, a);
startActivity(Intent.createChooser(share, "Select"));
Use this code to share image:
NOTE: you have to add WRITE/READ EXTERNAL STORAGE permission in Menifest file to do this:
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_SEND);
Log.d("imageUri", "imageUriIs" + uri);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.putExtra(Intent.EXTRA_SUBJECT, "");
intent.putExtra(Intent.EXTRA_TEXT, shareMSG);
intent.putExtra(Intent.EXTRA_TITLE, "TITLE");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "APPNAME"));

Save drawables in local storage as cache and then read them for a Share_Intent?

I have drawables in my project that i have declared as seperate resource array
public Integer[] mThumbIds = {
R.drawable.ic_bl1,
R.drawable.ic_bl2,
R.drawable.ic_bl3,
R.drawable.ic_bl4,
R.drawable.ic_bl5,
R.drawable.ic_ca1,
R.drawable.ic_ca2,
R.drawable.ic_ch}
I want to save them all in a temp folder on local storage of android(not SD Card)so that I can retrieve them later and send them via a share intent.I have code for share Intent completed
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, path);
// sharingIntent.setPackage("com.facebook.orca");
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(sharingIntent, "Send Via");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sContext.startActivity(chooserIntent);
// sContext.startActivity(sharingIntent);
}
});
I need to pass a Uri of a particular file to be sent,and best way to get it is through saving images in local storage and then retrieving there Uri.I know how to save a single image in cache and then retrieving it.
I can't figure out how to save a list of drawables in a particular folder on local storage and then retrieving there Uris for a use in share intent later.
You could use this code.
But add read and write storage permission.
public void onClick(View view) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
ImageView imageView = (ImageView) view;
Drawable mDrawable = imageView.getDrawable();
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(mContext.getContentResolver(), mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Content!!"));
}

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!"));
}

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