Delete Image after using Share Intent - android

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.

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

Share option not working for Marshmallow [android]

I fetch images from Json and display it in a gridview. On select an image, image enlarges to fullscreen and share option is available. As I click on share option, on phones above API 22, loading bar is not dismissing. It works well till Lollipop. Dont know why its not working in Marshmallow.
Here is the code
public void share(View view){
// Get access to the URI for the bitmap
pDialog = new ProgressDialog(Displaydemo.this);
pDialog.setMessage("Loading...");
pDialog.show();
niv1 = (NetworkImageView) findViewById(R.id.imgNetwork);
Uri bmpUri = getLocalBitmapUri(niv1);
if (bmpUri != null) {
pDialog.dismiss();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"share");
shareIntent.putExtra(Intent.EXTRA_TEXT,Datas.Address );
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
}
Looks like you are facing mashmallow Runtime permission issue. please refer to this post Android 6: cannot share files anymore?
Also if are using the v4 Support Library u should use ShareCompat for sharing instead https://medium.com/google-developers/sharing-content-between-android-apps-2e6db9d1368b#.wnlh9s3n7
String path = Environment.getExternalStorageDirectory() + File.separator + File.separator +file_name;
File file_ =new File(path);
private void shareFile( File file_) {
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setType(URLConnection.guessContentTypeFromName(file_.getName()));
intentShareFile.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://"+file_.getAbsolutePath()));
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}

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 content

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

Share intent for instagram in Android

Actually I want to share image in Instagram through intent.
I found this solution for images saved on SD card but I want to do same for image on site (link).
I tried with
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent
.putExtra(
Intent.EXTRA_STREAM,
Uri.parse("http://www.alliswell.biz/images/products/art/alliswell_signs/yellowB.jpg"));
shareIntent.setPackage("com.instagram.android");
startActivity(shareIntent);
But it's not working.
Edit
When I start above intent it opens my installed Instagram application and it immediately finish Instagram and toast message comes "unable to download file"
Actually it does not parse link and image respectively. What should be issue?
You should use local path to file
For example: "file:///path/to/file/image.png".
Note, that it is very important to include "file" in the path, without this part it also can show the same toast.
First of all you need to download file from that url. you may refer this code for downloading image from url:
String imageUrl = "Your_Image_Url";
if (imageUrl != null && !imageUrl.equals("")) {
String fileName = generateFileNameFromUrl(imageUrl);
String imageLocalPath = Environment.getExternalStorageDirectory()+ File.separator+"Your_App_Name"+ fileName;
if (!new File(imageLocalPath).exists()) {
ImageDownloadModel imageDownloadModel = new ImageDownloadModel();
imageDownloadModel.setImageLocalPath(imageLocalPath);
imageDownloadModel.setImageUrl(imageUrl);
imageDownloadModels.add(imageDownloadModel);
}
ImageLoadAsynkTask imageLoadAsynkTask = new ImageLoadAsynkTask(new ImageDownloadDelegate(), imageDownloadModels, albumDir, activity);
imageLoadAsynkTask.execute();
and then use uri for that image for sharing it on instagram:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imageLocalPath));
shareIntent.setPackage("com.instagram.android");
activity.startActivity(shareIntent);

Categories

Resources