i am new to android and i am trying to make my phone cam take a picture and display it in an image view. When the image is captured, it is saved correctly but after that the app stops working when the image should be displayed in an image view. Any help is appreciated. I searched some topics but still nothing works. Here is the code:
package myfirstapp.myapps.me.camera;
import ...
public class MainActivity extends ActionBarActivity {
ImageButton camBtn;
ImageView imageView;
ScrollView scrollView;
private File imageFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OpenCam(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"image.jpg");
Uri tempURI=Uri.fromFile(imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0)//==0 the same where startActivityForResult(intent, 0) so we are in the same process
{
switch (resultCode){
case Activity.RESULT_OK:
if(imageFile.exists())
{
Toast.makeText(MainActivity.this, "Image was saved at "+imageFile.getAbsolutePath(), Toast.LENGTH_SHORT)
.show();
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
}
else
{
Toast.makeText(MainActivity.this, "Image wasn't saved", Toast.LENGTH_SHORT)
.show();
}
break;
case Activity.RESULT_CANCELED:
Toast.makeText(MainActivity.this, "Image capture was cancelled", Toast.LENGTH_SHORT)
.show();
break;
}
}
}
}
Stacktrace
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {myfirstapp.myapps.me.camera/myfirstapp.myapps.me.camera.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:3410) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)
Please go through this Code:
And also,