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::

staticUricapturedImageUri=null;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView) this.findViewById(R.id.imageView1);
    ButtonphotoButton= (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(newView.OnClickListener() {

        @OverridepublicvoidonClick(View v) {
            Calendarcal= Calendar.getInstance();
            Filefile=newFile(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);
            Intenti=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
            startActivityForResult(i, CAMERA_RESULT);
        }
    });
    protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST) {
            //Bitmap photo = (Bitmap) data.getExtras().get("data");//imageView.setImageBitmap(photo);try {
                Bitmapbitmap= 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"

newFileOutputStream(getExternalStorageDirectory()+"filename");

OR

StringimageFilePath= 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"