Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save file to public (external) storage on Android Q (API 29)?

I have been using external storage in order to save a different type of files. That files needs to be visible to user. And now from Android Q, the method getExternalStoragePublicDirectory() has been deprecated getExternalStoragePublicDirectory(docs).

I used following code:

File externalFilesDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "DirectoryName");

Is there another way to save files which will be visible to user?

like image 942
t.jancic Avatar asked Aug 13 '19 10:08

t.jancic


People also ask

What is Android permission write external storage?

Android defines the following storage-related permissions: READ_EXTERNAL_STORAGE , WRITE_EXTERNAL_STORAGE , and MANAGE_EXTERNAL_STORAGE . On earlier versions of Android, apps needed to declare the READ_EXTERNAL_STORAGE permission to access any file outside the app-specific directories on external storage.


2 Answers

You need to add a check for Android Q. Let's assume you want to save the Image in your public Download folder.

try{
     OutputStream fos;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
       ContentResolver resolver = context.getContentResolver();
       ContentValues contentValues = new ContentValues();
       contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
       contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
       contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
       Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
       fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
    } else {
       String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
       File image = new File(imagesDir, fileName);
       fos = new FileOutputStream(image);
    }
     finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
     Objects.requireNonNull(fos).close();
}catch (IOException e) {
  // Log Message
}
like image 76
Nauman Ash Avatar answered Sep 30 '22 17:09

Nauman Ash


Save capture Bitmap in android Q and below verison

private class AsyncTaskExample extends AsyncTask<String, String, Bitmap> {
        Bitmap icon = null;
        ByteArrayOutputStream bytes;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mBitmap = BaseApplication.getInstance().getBitmap();
        ((BaseActivity)mContext).showFullScreenProgressLoader(((BaseActivity)mContext), ((BaseActivity)mContext).getString(R.string.please_wait), ((BaseActivity)mContext).getString(R.string.we_are_processing_your_request));
    }

    @Override
    protected Bitmap doInBackground(String... strings) {
        try {



      String imageFileName = "picture_" + System.currentTimeMillis() + ".jpg";
                File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + Constants.INTERNAL_FOLDER_NAME + "/");
                directory.mkdirs();
                mFile = new File(directory, imageFileName);
                if (!mFile.isFile()) {
                    try {
                        mFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
                    final ContentResolver resolver = ((CameraFaceDetectionActivity) mContext).getContentResolver();
                    insertImage(resolver, mBitmap, mFile.getAbsolutePath(), "");
                } else {
                    FileOutputStream out = new FileOutputStream(mFile);
                    try {
//                    mBitmap = modifyOrientation(mBitmap,mFile.getAbsolutePath());
                        OutputStream output = new FileOutputStream(mFile);
                        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
                        output.flush(); // Not really required
                        output.close();
                    }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }




        } catch(Exception e){
            e.printStackTrace();
        }
        return mBitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    OutputStream output = new FileOutputStream(mFile);
                    Bitmap b = modifyOrientation(mBitmap, mFile.getAbsolutePath());
                    b.compress(Bitmap.CompressFormat.JPEG, 100, output);


                } catch (IOException e) {
                    e.printStackTrace();
                }

                startActivity(new Intent(mContext, SelfiePreviewForgetPsdActivity.class)
                        .putExtra(Constants.KEY_MOBILE, mMobileNumber)
                        .putExtra(Constants.KEY_COUNTRY_CODE, mCountryCode)
                        .putExtra(KEY_ID_NUMBER, idNumber)
                        .putExtra(Constants.KEY_USER_DETAIL, mVerifyUserDetailResponse)
                        .putExtra(KEY_ID_TYPE, idType)
                        .putExtra(Constants.IMAGE_PATH, mFile.getAbsolutePath()));
            }
        }, 500);
    }
}

below are the method for save bitmap on android Q for specify folder

public static String insertImage(ContentResolver cr,
                                 Bitmap source,
                                 String title,
                                 String description) {

    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, title);
    values.put(MediaStore.Images.Media.DISPLAY_NAME, title);
    values.put(MediaStore.Images.Media.DESCRIPTION, description);
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
    // Add the date meta data to ensure the image is added at the front of the gallery
    values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());

    Uri url = null;
    String stringUrl = null;    /* value to be returned */

    try {
        url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        if (source != null) {
            OutputStream imageOut = cr.openOutputStream(url);
            try {
                source.compress(Bitmap.CompressFormat.JPEG, 100, imageOut);
            } finally {
                imageOut.close();
            }

            long id = ContentUris.parseId(url);
            // Wait until MINI_KIND thumbnail is generated.
            Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null);
            // This is for backward compatibility.
            storeThumbnail(cr, miniThumb, id, source.getWidth(), source.getHeight(), MediaStore.Images.Thumbnails.MICRO_KIND);
        } else {
            cr.delete(url, null, null);
            url = null;
        }
    } catch (Exception e) {
        if (url != null) {
            cr.delete(url, null, null);
            url = null;
        }
    }

    if (url != null) {
        stringUrl = url.toString();
    }

    return stringUrl;
}

Save bitmap method

 private static final Bitmap storeThumbnail(
        ContentResolver cr,
        Bitmap source,
        long id,
        float width,
        float height,
        int kind) {

    // create the matrix to scale it
    Matrix matrix = new Matrix();

    float scaleX = width / source.getWidth();
    float scaleY = height / source.getHeight();

    matrix.setScale(scaleX, scaleY);

    Bitmap thumb = Bitmap.createBitmap(source, 0, 0,
            source.getWidth(),
            source.getHeight(), matrix,
            true
    );

    ContentValues values = new ContentValues(4);
    values.put(MediaStore.Images.Thumbnails.KIND, kind);
    values.put(MediaStore.Images.Thumbnails.IMAGE_ID, (int) id);
    values.put(MediaStore.Images.Thumbnails.HEIGHT, thumb.getHeight());
    values.put(MediaStore.Images.Thumbnails.WIDTH, thumb.getWidth());

    Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values);

    try {
        OutputStream thumbOut = cr.openOutputStream(url);
        thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
        thumbOut.close();
        return thumb;
    } catch (Exception ex) {
        return null;
    }
}
like image 40
Vinod Makode Avatar answered Sep 30 '22 16:09

Vinod Makode