This fist method is working fine but I am not able to save this image file to a cache. Like I want to save it temporarily.
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BitmapDrawable drawable = (BitmapDrawable) ivMeme.getDrawable();
Bitmap b = drawable.getBitmap();
//Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.refer_pic);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "MEME", "Just A MEME");
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
share.putExtra(Intent.EXTRA_TEXT, "Shared via MemeSharing App");
startActivity(Intent.createChooser(share, "Share this Meme using"));
}
});
I am not able to convert this method to save file at getExternalCacheDir()
This second Method Just able to save file locally but not able to share that image file. As goes for share app goes down.
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BitmapDrawable drawable = (BitmapDrawable) ivMeme.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File filepath= Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath()+"/Demo/");
Log.d("Error:01",dir.getAbsolutePath());
dir.mkdir();
File file = new File(dir,System.currentTimeMillis()+".jpg");
try {
Toast.makeText(MainActivity.this,"InSide Try ",Toast.LENGTH_SHORT).show();
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
Log.d("Error:1",e.getMessage());
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
Toast.makeText(MainActivity.this,"Saved Successfully "+file.getAbsolutePath(),Toast.LENGTH_LONG).show();
try {
outputStream.flush();
} catch (IOException e) {
Log.d("Error:2",e.getMessage());
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
Log.d("Error:3",e.getMessage());
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(share,"Share via"));
}
});
I don't know why second method is not working? I have tried both method for sharing Image of ImageView, only one of them is working.