In my android app I have activity where when user clicks a button it opens camera by using intent, takes a photo, shows it as a image view in the same activity and after that there is another button for sending that photo by Whatsapp. **I think I am not saving file correctly.
Please have look at section where i am saving file and giving file a name and after when send by whatsapp button, it opens up whatsapp but there is no image attached and whatsapp gives error.**
Here is my activity code.
public class Main2Activity extends AppCompatActivity {
ImageView iv;
File imagesFolder;
File image;
Uri uriSavedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
iv = (ImageView) findViewById(R.id.imageView3);
Button btnCapture = (Button) findViewById(R.id.button_camera);
Button send = (Button) findViewById(R.id.send_whatsapp);
//Set listener on Capture button
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent c = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Implicit Intent
startActivityForResult(c, 0);
imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
image = new File(imagesFolder, "image_001.jpg");
String fileName = image.toString();
Log.e("log for file name", "hello");
uriSavedImage = Uri.fromFile(image);
c.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
}
});
}
//Override method onActivityResult used to retreive the image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap m = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(m);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
public void sendSelfie(View view) {
Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.setType("image/jpg");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
startActivity(Intent.createChooser(whatsappIntent, "Share image using"));
}
}
My XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.happybirthdayprathmesh.Main2Activity">
<Button
android:id="@+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="clickSelfie"
android:text="Click here to take photo" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="60dp">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<Button
android:id="@+id/send_whatsapp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="sendSelfie"
android:text="Send by Whatsapp" />
</RelativeLayout>