Skip to content Skip to sidebar Skip to footer

Take Picture With Camera Intent And Save To File

Possible Duplicate: Android Camera - Save image into a new folder in SD Card i'm trying to take picture and save it to a file. The problem cames i'm trying to save the bitmap to

Solution 1:

ry the below code is one of the solution to your problem::

static Uri capturedImageUri = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView) this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Calendar cal = Calendar.getInstance();
            File file = new File(Environment.getExternalStorageDirectory(), (cal.getTimeInMillis() + ".jpg"));
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                file.delete();
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            capturedImageUri = Uri.fromFile(file);
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
            startActivityForResult(i, CAMERA_RESULT);
        }
    });
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST) {
            //Bitmap photo = (Bitmap) data.getExtras().get("data");
            //imageView.setImageBitmap(photo);
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), capturedImageUri);
                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

Solution 2:

your error clearly says that java.io.FileNotFoundException: /filename

please provide exact path "/sdcard/filename"

new FileOutputStream(getExternalStorageDirectory()+"filename");

OR

String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/name.png";

note: add permission WRITE_EXTERNAL_STORAGE in manifest file.


Post a Comment for "Take Picture With Camera Intent And Save To File"