I try to write an apps to take photo, save the photo at my desire directory with my desired name, bitmap the photo then place at the ImageReview. I can work well in first photo taking, photo saving, photo bitmap and placing image at Image Review. While I try to redo this 2nd time, I still can launch the camera and take photo, but when saving the photo, i face issue with message "Unfortunately, XXXXXX has stopped". Need helps to resolve this. Thanks.
I suspect the command which cause this problem is as below because when i commented out this command line, I don't face this problem in 2nd time photo saving: cameraBox1.setImageBitmap(captureBmp);
Do I need to add anything to manage this?
Here are my coding:
takeButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image = getFile(MainActivity.this);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image) );
startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);
}
});
public File getFile(Context context){
final File path = new File( Environment.getExternalStorageDirectory(), "My Image" );
if(!path.exists()){
path.mkdir();
}
String name;
int n = 100000;
int rand;
rand = new Random().nextInt(n);
name = "Image-" + rand + ".jpg";
File fileimage = new File(path, name);
return fileimage;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK){
Toast.makeText(this, "Image Captured", Toast.LENGTH_LONG).show();
switch(requestCode){
case CAPTURE_IMAGE_CAPTURE_CODE:
try {
Bitmap captureBmp;
captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(image) );
ImageUri = Uri.fromFile(image);
String pathToImage = ImageUri.getPath();
cameraBox1 = (ImageView) findViewById(R.id.cameraBox);
cameraBox1.setImageBitmap(captureBmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
cameraBox1.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
cameraBox1.setAdjustViewBounds(true);
break;
}
}
else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
API That I use:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.annotation.SuppressLint;