1

I'm trying to take a screenshot of a SurfaceView, but it always results of a black screen. I've been searching but all the solutions I've found doesn't work to me. This is my code.

This is the method I call to take the screenshot

private void takePicture(){
        if(camera!=null) {
            camera.takePicture(null, null, jpgCb);
        }
    }

This is the jpg callback

jpgCb=new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(final byte[] data, final Camera camera) {
            btnAccept.setVisibility(View.VISIBLE);
            btnCancel.setVisibility(View.VISIBLE);
            btnFlash.setEnabled(false);

            btnAccept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    stopCamera();
                    new AsyncTask<byte[], String, Void>() {
                        @Override
                        protected void onPreExecute() {
                            progressDialog=new ProgressDialog(CameraActivity.this);
                            progressDialog.setCancelable(false);
                            progressDialog.setTitle(getResources().getString(R.string.saving_process));
                            progressDialog.show();
                        }

                        @Override
                        protected void onProgressUpdate(String... values) {
                            progressDialog.setMessage(values[0]);
                        }

                        @Override
                        protected Void doInBackground(byte[]... params) {
                            byte[] data=params[0];

                            File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator+"folder"+File.separator+folderName);

                            if (!fileDir.exists())
                                fileDir.mkdirs();

                            File tmpFile=new File(fileDir,"tmpFile.jpeg");

                            try{
                                FileOutputStream fos=new FileOutputStream(tmpFile);
                                fos.write(data);
                                fos.close();
                            }catch (FileNotFoundException e){
                                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
                            }catch (IOException e){
                                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
                            }


                            BitmapFactory.Options options=new BitmapFactory.Options();
                            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

                            tmpFile.delete();
                            takeScreenshot();

                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void aVoid) {
                            super.onPostExecute(aVoid);

                            try{
                                pics=loadPics();
                                setGallery();
                            }catch (NullPointerException e){
                                e.printStackTrace();
                            }

                            Toast.makeText(getApplicationContext(),getResources().getString(R.string.image_saved),Toast.LENGTH_SHORT).show();

                            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                                startCamera(w,h);
                            }else
                                startCamera();
                            btnAccept.setVisibility(View.GONE);
                            btnCancel.setVisibility(View.GONE);
                            btnFlash.setEnabled(true);
                            progressDialog.dismiss();
                        }
                    }.execute(data);
                }
            });

            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopCamera();
                    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                        startCamera(w,h);
                    }else
                        startCamera();
                    btnAccept.setVisibility(View.GONE);
                    btnCancel.setVisibility(View.GONE);
                    btnFlash.setEnabled(true);
                }
            });
        }
    };

And this is the method I call in the callback that really takes the screenshot

public void takeScreenshot(){
    cameraContainer.setDrawingCacheEnabled(true);
    cameraContainer.buildDrawingCache(true);
    Bitmap bmp=Bitmap.createBitmap(cameraContainer.getDrawingCache());
    cameraContainer.setDrawingCacheEnabled(false);
    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG,qualityValue,bos);
    byte[] bitmapData=bos.toByteArray();
    ByteArrayInputStream fis=new ByteArrayInputStream(bitmapData);

    String fileName=System.currentTimeMillis()+".jpeg";
    File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator+"folder"+File.separator+folderName);

    if (!fileDir.exists())
        fileDir.mkdirs();

    try{
        File tmpFile=new File(fileDir,fileName);
        FileOutputStream fos=new FileOutputStream(tmpFile);

        byte[] buf=new byte[1024];
        int len;
        while ((len=fis.read(buf))>0) {
            fos.write(buf, 0, len);
        }
        fis.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

With this I'm able to save the "SurfaceView content" in a file and store it, but the content is a fully black screen.

PS: This is the question I most find when trying to solve my problem (indeed, the 'takeScreenshot' method is from that answer)

EDIT: This is the stopCamera method

private void stopCamera(){
    camera.setPreviewCallback(null);
    camera.stopPreview();
    camera.release();
    camera=null;
}
Alex
  • 750
  • 5
  • 15
  • 33

1 Answers1

0

Well, after you shut down your camera, i'd expect the camera to not show anything anymore, so the surface should be empty, means black.

What do you want to achieve, anyway? You already got your photo saved to a file already, didn't you?

Bondax
  • 3,143
  • 28
  • 35