Sharing content - android

I have a gridview, clicking any image in the gridview expands it to a bigger image view, in this image view i can swipe left and right to change my images, i have added a method that would let me share the image to other apps like facebook watsapp etc. But the method seems to be not quite right, i cant figure out my mistake. It opens the popup window showing options to share the image but when i choose any option to send the image it says, Couldn't Load The Image.
Have a look at my code:
else {
if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
URL url2 = new URL((thumb[j]));
Bitmap bimage = BitmapFactory.decodeStream(url2
.openConnection().getInputStream());
shareImage();
Toast.makeText(getApplicationContext(),
"Saving to Favorites", Toast.LENGTH_LONG)
.show();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
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 bimage = Environment.getExternalStorageDirectory()
+ thumb[j];
File imageFileToShare = new File(bimage);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
}
Whereas thumb is the array having all the resource ids of the image, "j" is the int who's value initiated to 0.
Using the gesture to initiate the code.
I am ready to share my full code for greater understanding.
Any help would highly be appreciated.

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(Images.Media.EXTERNAL_CONTENT_URI + "/" + imageIDs);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));

Related

How to send any image file to a particular whatsapp contact through Custom Url Scheme

I got success in sending text to a particular WhatsApp contact using this code
private void openWhatsApp(String mobileNumber) {
String text = "Hi";
if(whatsappInstalledOrNot("com.whatsapp")){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("whatsapp://send?text="+text+ "&phone="+mobileNumber));
startActivity(browserIntent);
}else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
Now I want to share imageFile along with the text. I got the file path but don't know how to put in that browserIntent. Please Help.
I know its too late to answer but incase any one wondering.
Here is how you can use intent to share image file on Whatsapp
Intent share = new Intent();
// 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 = file; // here path to your image file
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
Log.e("media", String.valueOf(uri));
share.setPackage("com.whatsapp");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(share);
PS NOTE: You cannot send image with text as I tried some but fail to achieve it but you can share image file only from above code

Google+ share dialog show wrong image

I'm trying to share image and title on Google+ using this method:
share(com.google.android.apps.plus, img, title);
====================================
private void share(String type, int imgResId, String title) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = context.getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase(
Locale.ENGLISH).contains(type)
|| info.activityInfo.name.toLowerCase(
Locale.ENGLISH).contains(type)) {
Log.d("pack", info.toString());
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, title);
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(Helper
.saveFile(context, imgResId))); // Optional,
// just if you
// wanna share
// an image.
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
context.startActivity(Intent.createChooser(share, "Select"));
}
}
when Google+ share dialog comes up it shows wrong image(the image I try to share on Google+ the first time).I try to change the image resource id but still the same wrong image.
I ran into the same issue when trying to reuse a URI for different files, there appears to be an image cache somewhere that doesn't notice that the image changed.
I "fixed" it by generating a different URI for a new image (in your case, use a unique filename each time). A better solution would be to invalidate that cache, but I'm not aware of how (or if) that can be done.

Delete Image after using Share Intent

Hello I am new to android programming.
I have made an app in which images can be shared on various apps. Everything works fine but after the image is shared the images are saved in the phone. The images are saved in different folders on different devices.
I Want the images to be deleted after the images are shared.
Any help would be appreciated
Below is the onClick of the Share button.
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ImageView Imgv = (ImageView)viewPager.findViewWithTag(viewPager.getCurrentItem());
Drawable mDrawable = Imgv.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = Images.Media.insertImage(getContentResolver(),
mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share sticker via"));
}
});
This works for me,try this way to delete image
File fdlt = new File(file_path);
if (fdlt .exists()) {
if (fdlt .delete()) {
System.out.println("File Deleted :" + file_path);
} else {
System.out.println("File not Deleted :" + file_path);
}
}
Run a Asynctask which will wait for a Time and then it will delete the Image,
Execute this Asynctask when you will come to your app after sharing that Image.
For This use
startActivityForResult(Intent,int)
and Override
OnActivityResult
and Check if you are coming back from sharing the Image with Particular request code and then execute the asynctask.
It will delete image after a time or you also can delete image immediately on OnActivityResult.
Hope it Helps.

Send bitmap to image picker / gallery application

I'm trying to add an option in my app to send an image inside my app to other app like gallery or image picker/selectors. For example, a third-party launcher, which wants to select an icon that is inside my app.
I'm using a RecyclerView grid, to show the list of icons.
This is my code to load the icons:
icons = mContext.getResources().getStringArray(R.array.icons);
list = new ArrayList<String>(Arrays.asList(iconNames));
loadIcon(list);
private void loadIcon(List<String> list) {
mThumbs = new ArrayList<>();
for (String extra : list) {
int res = mContext.getResources().getIdentifier(extra, "drawable", p);
if (res != 0) {
final int thumbRes = mContext.getResources().getIdentifier(extra, "drawable", p);
if (thumbRes != 0)
mThumbs.add(thumbRes);
}
}
}
And this is the code in the OnClick of grid item:
Intent intent = new Intent();
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeResource(mContext.getResources(), mThumbs.get(position));
} catch (Exception e) {
Log.v("Icons Picker Error", "Picker error: " + Log.getStackTraceString(e));
}
if (bitmap != null) {
intent.putExtra("icon", bitmap);
mContext.setResult(Activity.RESULT_OK, intent);
} else {
mContext.setResult(Activity.RESULT_CANCELED, intent);
}
mContext.finish();
Apparently, icon bitmap is created properly, but when trying to send it to the selector, nothing is sent.
I'm not sure what could be wrong in the code, but I hope someone could help me with this. Thanks in advance.
you can display a chooser (all apps capable of performing the task are displayed for the user to select from). here is a code snippet to do that:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Choose an app to share the image"));
For this to work, you need to pass the Uri of the bitmap. One way of achieving this is to save the bitmap in a temporary file and then use FileProvider.getUriForFile() to get the uri of the temp file.
good luck
clive

sharing image with whatsapp in android

I have image in assets folder and need to share it with whatsapp application
I tried this code , it keeps give me sharing failed try again ! what's wrong ?!
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.setPackage("com.whatsapp");
// share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("file:///assets/epic/adv.png")));
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/epic/adv.png"));
this.startActivity(Intent.createChooser(share, "share_via"));
This code for share image via whatsapp worked fine for me .
public void shareImageWhatsApp() {
Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.adv);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
new FileOutputStream(f).write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse( Environment.getExternalStorageDirectory()+ File.separator+"temporary_file.jpg"));
if(isPackageInstalled("com.whatsapp",this)){
share.setPackage("com.whatsapp");
startActivity(Intent.createChooser(share, "Share Image"));
}else{
Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
}
}
private boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
You have several problems.
First, file:///assets/ is not a valid Uri on any version of Android. Your own application can refer to its own assets via file:///android_asset/.
Second, only you can access your own assets via file:///android_asset/ -- you cannot pass such a Uri to third-party apps. Either copy the file from assets into internal storage and use FileProvider, or you can try my StreamProvider and try to share the data straight out of assets/.
Third, there is no guarantee that com.whatsapp exists on the device, or that com.whatsapp will support ACTION_SEND of file:/// Uri values with a MIME type of image/*, and so you may crash with an ActivityNotFoundException.
Fourth, the user might want to share this image via some other means than WhatsApp. Please allow the user to share where the user wants, by removing the setPackage() call from your Intent.
This worked for me
Bitmap imgBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.image);
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,"title",null);
Uri imgBitmapUri=Uri.parse(imgBitmapPath);
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
startActivity(Intent.createChooser(shareIntent,"share image using"));

Categories

Resources