I currently have an app in which I'm taking photos and I'm trying to share them directly through the app. The problem I'm having is that when the chooser comes up and I pick one, I get an error toast that pops up and says "Unable to attach file". Then it just takes me back into the app and everything else still works. I'm assuming that this means that I have to save the image first, but I'm not sure if I'm doing that correctly. I found some examples online of how to save the images but I'm not sure if that's the problem or if the share intent is the problem.
final Button shareButton = (Button) findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", "description");
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap image = bitmapDrawable.getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
});
I have "title" and "description" in as placeholders until I can get this to work.