1

Is there alternative to getExternalStorageDirectory() since it is deprecated in Android Level Q? Please provide an example using that.

Edit 1: I have used following code to save my camera stream to storage

            Image image = null;
            image  = reader.acquireLatestImage();
            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
            byte[] bytes = new byte[buffer.capacity()];
            buffer.get(bytes);
            ContentValues values = new ContentValues();

            values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis() / 1000);
            values.put(MediaStore.MediaColumns.DATE_TAKEN, System.currentTimeMillis());
            values.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
            values.put(MediaStore.MediaColumns.IS_PENDING, true);
            values.put(MediaStore.MediaColumns.DISPLAY_NAME, "file_" + System.currentTimeMillis() + extension);
            Uri uri = getContentResolver().insert(contentUri, values);

            try {
                uri = save(bytes,uri);
                values.put(MediaStore.MediaColumns.IS_PENDING, false);
                getContentResolver().update(uri, values, null, null);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        if(image!=null)
                        {
                            image.close();
                        }
                    }

But this does not save image to storage on pressing camera button one time, On pressing capture button fastly for two times it saves it to storage. Are there any errors here?

Coder
  • 129
  • 7

1 Answers1

0

Check android sdk version. And please use getExternalFilesDir()

if (android.os.Build.VERSION.SDK_INT >= android.os. Build.VERSION_CODES.Q) {
   // only for up to android Q
   // Use getExternalFilesDir()
   ...
} else { 
   // Use getExternalStorageDirectory()
   ...
}
naijab.com
  • 508
  • 6
  • 10
  • But getExternalFilesDir() create folder like Internal_Storage->Android->data->com.example.appname->files. I want to create something like Internal_Storage->App_Name->Pictures or sounds or videos – Coder Apr 10 '20 at 08:07
  • @Coder have you try https://stackoverflow.com/a/40727170/7575602 – naijab.com Apr 10 '20 at 08:22
  • Yup. But it uses getExternalStorageDirectory() thereby causing error in my phone since it has android 10 – Coder Apr 10 '20 at 08:30