I wanted to share images with your own app to all " Instagram ", " WhatsApp " or maybe using any social media app?
As I got the solution for whatsapp, I still couldn't get the solution for Instagram.
My code is as follows:
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp_"+n+".jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
String savedFile = saveImageFile(bitmap, "myFolder");
Uri imageUri = Uri.parse(savedFile);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Share Image"));
}
});
}
savedImages method:-
public static String saveImageFile(Bitmap image, String folder){
String now = Long.toString(new Date().getTime());
File imageFile = new File(Environment.getExternalStorageDirectory()+"/" + folder);
if (!imageFile.exists()){
File screenShotsFolder = new File(Environment.getExternalStorageDirectory()+"/"+ folder+ "/");
screenShotsFolder.mkdirs();
}
File imageName = new File(new File(Environment.getExternalStorageDirectory() +"/"+ folder + "/"), now + ".jpg" );
try{
FileOutputStream outputStream = new FileOutputStream(imageName);
image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
}catch (Throwable e){e.printStackTrace();}
return imageName.toString();
}
This code is working for " WhatsApp " but not for " Instagram "
In Instagram, I am not able to send to particular person like " 9gag ".
Use,
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(f.getPath()));
instead of Environment.getExternalStorageDirectory().getPath())
but for nougat and above you should use FileProvider
to get the file uri..
picUri = FileProvider.getUriForFile(RegisterActivity.this , this.getApplicationContext().getPackageName() + ".provider", f);
Here is the complete code. Change your code to the following
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String savedFile = saveImageFile(bitmap, "myFolder");
Uri imageUri = Uri.parse(savedFile);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Share Image"));
}
});
Saving Image file function
public static String saveImageFile(Bitmap image, String folder){
String now = Long.toString(new Date().getTime());
File imageFile = new File(Environment.getExternalStorageDirectory() + folder);
if (!imageFile.exists()){
File screenShotsFolder = new File("/sdcard/Pictures/" + folder+ "/");
screenShotsFolder.mkdirs();
}
File imageName = new File(new File("/sdcard/Pictures/" + folder + "/"), now + ".jpg" );
try{
FileOutputStream outputStream = new FileOutputStream(imageName);
image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
}catch (Throwable e){e.printStackTrace();}
return imageName.toString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.