如何将文件保存到安卓Q(API 29)上的公共(外部)存储?

2022-09-02 22:30:15

我一直在使用外部存储来保存不同类型的文件。该文件需要对用户可见。现在从Android Q开始,该方法已被弃用getExternalStoragePublicDirectory(docs)。getExternalStoragePublicDirectory()

我使用了以下代码:

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

有没有另一种方法可以保存对用户可见的文件?


答案 1

您需要为Android Q添加检查。假设您要将映像保存在公共下载文件夹中。

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
}

答案 2

在 android Q 及以下版本中保存捕获位图

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);
    }
}

以下是在Android Q上保存位图的方法,用于指定文件夹

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;
}

保存位图方法

 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;
    }
}

推荐