0

I am trying to save the preview frames from the camera on the external storage of my device.

These are the permissions in my Manifest:

<uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And this is the Callback inside the surfaceChanged():

  mCamera.setPreviewCallback(new PreviewCallback() {

                public void onPreviewFrame(byte[] data, Camera camera) {
                    Camera.Parameters parameters = camera.getParameters();
                  Size size = camera.getParameters().getPreviewSize();
                  frames++;
                  Log.v("Framecount", "Frame: " + frames + "Größe: " + data.length);

                  int width = parameters.getPreviewSize().width;
                  int height = parameters.getPreviewSize().height;

                  ByteArrayOutputStream outstr = new ByteArrayOutputStream();
                  Rect rect = new Rect(0, 0, width, height); 
                  YuvImage yuvimage=new YuvImage(data,ImageFormat.NV21,width,height,null);
                  yuvimage.compressToJpeg(rect, 100, outstr);
                  Bitmap bmp = BitmapFactory.decodeByteArray(outstr.toByteArray(), 0, outstr.size());
                  Bitmap thumbnail = Bitmap.createScaledBitmap(bmp, 20, 20, false);
                try   {
                    File sdCard = Environment.getExternalStorageDirectory();
                    File dir = new File (sdCard.getAbsolutePath() + "/.aaaaaaaaa/");
                    dir.mkdirs();
                    File file = new File(dir, "filename.jpg");
                    FileOutputStream output = new FileOutputStream(file);
                  yuvimage.compressToJpeg(rect, 90, output);
                  output.flush();
                  output.close();
                }
                catch(Exception e) {
                    Log.v("Error", "Error");
                }



                }
              });

Showing the Preview frames on the screen is not the problem, I just can't save them. Does anyone have an idea what could be wrong?

SparklingWater
  • 358
  • 4
  • 15
  • `Log.v("Error", "Error");`. That does not tell you which exception happened. Better: `Log.v("Exception", e.getMessage());`. Also add an `e.printStackTrace();` to that catch block. Please tell which Exception you get. – greenapps Jun 08 '15 at 17:12
  • `dir.mkdirs();`. No not like that. Better: `if (!dir.exists()) dir.mkdirs();`. And that is not fully ok too as you should inspect the return value of mkdirs() too and not continue if it fails. Display a toast saying so. Maybe a Toast is not allowed. If so just Log it and return. – greenapps Jun 08 '15 at 17:14
  • Thanks for the tips: Logcat gives me the following errors with the tag mm-camera: isp_util_hw_notify_stats: TLDEBUG tintless frame id 1370, isp_tintless_bg_pca_stat_config: pointer okay, isp_tintless_bg_pca_stat_config: lib returned config err=0, dmlroc_entry entering dmlroc_entry – SparklingWater Jun 08 '15 at 17:39
  • As you have discovered you will get many frames. So better change `File file = new File(dir, "filename.jpg");` to `File file = new File(dir, "filename." + frames + ".jpg");`.Your code works for me by the way. – greenapps Jun 08 '15 at 17:39
  • Sorry i do not understand the errors you posted. What does it have to do with saving to file or Exceptions from that catch block? And i like to know the result of exists() and mkdirs(). How did you check that that hidden directory does not exist? – greenapps Jun 08 '15 at 17:40
  • After making all the changes you recommended: !dir.exists() returns false, and it doesn't seem like there is an exception being thrown, Like you said it works for you and there are no errors, but I can't find the files on my sdcard. – SparklingWater Jun 08 '15 at 17:48
  • `dir.exists() returns false,` Unclear!. Is that after mkdirs()? You should check it after mkdirs too if you do not return. Please edit your post here to show all exists() and mkdirs() code too. – greenapps Jun 08 '15 at 17:50
  • `but I can't find the files on my sdcard.` Well how are you looking for them? And most probably they are not on a removable micro SD card but just on external memory of the device. Look at the path given by `file.getAbsolutePath()` – greenapps Jun 08 '15 at 17:50
  • The Gallery app seems to cache the folder structure,so I just restarted my phone and now I am able to see the frames. Thank you for your help! – SparklingWater Jun 08 '15 at 17:55
  • No. This code will not care for those pictures beeing visible in the Gallery. Only after reboot of your device this is possible. You should of course use a File Explorer app. In it's settings check 'show hidden files'. . Or do not use a hidden folder for a start. – greenapps Jun 08 '15 at 17:57
  • If you want your jpg's immediately visible in the Gallery then invoke the media scanner on every new file. Search for the two lines of code on this site. – greenapps Jun 08 '15 at 17:59

1 Answers1

0

If you want your jpg's immediately visible in the Gallery then invoke the media scanner on every new file. Search for the two lines of code on this site.

greenapps
  • 11,154
  • 2
  • 16
  • 19