I want to share image on share button. I use Glide to set image on ImageView. then I want to share particular image at that time I got The file format is not supported error.
private void shareContent(ImageView imageView) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Bitmap bitmap =getBitmapFromView(imageView);
try {
File file = new File(context.getExternalCacheDir(),"logicchip.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_TEXT, "test");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
context.startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
}
private Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
bgDrawable.draw(canvas);
} else{
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}
3
Answers
You should try to use the bitmap you have generated via glide, don’t try to take it from the view
Do not use Uri.fromFile() but use FileProvider to get an uri and serve your file.
You can use this code for image sharing. also use path provider in your manifest.
path provider in manifest inside application tag