7

I coded taking picture and saving it in external storage.It works well on my tablet with a sd card.However,I get some trouble with my phone which is not support sd card.Can anyone help me to write codes to save in internal storage.Thanks!

    ((Button) findViewById(R.id.btnTaken))
            .setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                            takeImage();                            
                }
                });



protected void takeImage() {
     Log.i(TAG,"onClick event");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String currentDateandTime = sdf.format(new Date());
        String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() +
                               "/Image_" + currentDateandTime + ".jpg";


        mCamera.takePicture(fileName);
        Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();


}
public void takePicture(final String fileName) {
    Log.i(TAG, "Tacking picture");
    PictureCallback callback = new PictureCallback() {

        private String mPictureFileName = fileName;

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.i(TAG, "Saving a bitmap to file");
            Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
            try {
                FileOutputStream out = new FileOutputStream(mPictureFileName);
                picture.compress(Bitmap.CompressFormat.JPEG, 90, out);
                picture.recycle();
                mCamera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    Camera.ShutterCallback shutter = new Camera.ShutterCallback() {
         public void onShutter() {
         Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
         }
         };
         Camera.PictureCallback raw = new Camera.PictureCallback() {
             public void onPictureTaken(byte[] data, Camera c) {
              Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
              mCamera.startPreview();
             }
             };
    mCamera.takePicture(shutter, raw, callback) ;



}
user2205173
  • 71
  • 1
  • 4

1 Answers1

-4

This is Very Simple..Instead of External file path name..You should give the Internal File Path name..Which is FileOutputStream out = new FileOutputStream(new File(getFilesDir()+File.separator+"MyFile.jpg"));..Ple try it..Its working good

and

One more thing,You should add the permission for internal storage into Android Manifest.xml file

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
Arun Kumar
  • 100
  • 1
  • 2
  • 12
  • 2
    That permission doesn't exist, and an OutputStream is not a file name. – clemp6r Mar 06 '14 at 16:34
  • I am also known about outputstream. That is a basic thing yaar. From that 'out' variable you can write the data. Have u known about write the Data in OutPutStream??? – Arun Kumar Mar 07 '14 at 06:20