The following is code to take a picture using the camera in my app. On pressing the imgButton
a picture is supposed to be taken and displayed in the imageView
.
What happens is, when I press the button, the camera is opened. However the image taken is not displayed in the image view
On debugging it appears that if (imgFile.exists())
in the method onActivityResult()
is coming out false and the statements inside if
are not being executed.
MainActivity.java
package com.example.nirvan.cameraexample3;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity
{
private String pictureImagePath = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imgButton=(Button) findViewById(R.id.imgButton);
View.OnClickListener imgButtonClickListener=new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(pictureImagePath);
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 1);
}
};
imgButton.setOnClickListener(imgButtonClickListener);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("TAG","CUSTOOOM");
if (requestCode == 1)
{
File imgFile = new File(pictureImagePath);
if (imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageViewTest);
myImage.setImageBitmap(myBitmap);
}
}
}
}
MainActivity.xml
<?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.nirvan.cameraexample3.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewTest"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IMG"
android:id="@+id/imgButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp" />
</RelativeLayout>
There are some issue
1.) Move your listener creation outside
oncreate
2.) Use the already created image file uri instead of creating the new one
3.) Confirm the
RESULT_OK
confirmation tooYou need to add permission in your manifest too
Or if your app supports Marshmallow and above then you need to implement
Implement Run-time permission