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"));
Related
I am trying to share an GIF programmatically, but it is getting shared as an image (not animated)
Code Snippet:
Intent shareIntent = new Intent();
shareIntent.setType("image/gif");//image/gif
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(myList.get(item)));
startActivity(Intent.createChooser(shareIntent,"Share gif"));
You are trying to share to which apps?
Some whatsapp builds don't support gif feature.
You can check this question here with some additional information: How to share GIF image in Whatsapp programmatically in Android?
There is this comment:
"This solution doesn't work with whatsapp but does work with all the other apps I specified above (namely Hangouts, Android Messages, Facebook Messenger)"
He is referring to this answer:
private void ShareGif() {
// replace the path and file name with your gif image path and name
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "temporary_file.gif";
File sharingGifFile = new File(baseDir, fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/gif");
Uri uri = Uri.fromFile(sharingGifFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share image"));
}
/*
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(sharePath));
shareIntent.setType("image/*");
startActivity(shareIntent);
*/
Hope it helps.
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!!"));
}
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:"));
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.
I am creating a photo application where the user selects and image from the gallery and it displays in an imageview in the center of the app. How would I go about creating a share intent for the ImageView?
UPDATE
Share intent is working, however, the image can not be shared because I can not save it to the path I have selected. Below is my code. Any help?
public void taptoshare(View v)
{
View content = findViewById(R.id.myimage);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/DCIM/Camera/image.jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
shareIntent.setData(phototUri);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
}
Try this
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(path);
shareIntent.setData(photootUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, photootUri);
getContext().startActivity(Intent.createChooser(shareIntent, "Use this for sharing"));
See the below link. it will very helpful for u. I think it satisfied your requirement.
http://www.technotalkative.com/android-pick-image-from-native-gallery/