如何将图像保存到Android Q中的camera文件夹中? [英] How to save image to camera folder in Android Q?

查看:68
本文介绍了如何将图像保存到Android Q中的camera文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像保存到camera文件夹,但是由于不赞成使用Android Q getExternalStoragePublicDirectory,所以我用另一种方式来做.我所拥有的(此方法接收位图及其名称):

I need to save an image to camera folder, but as Android Q getExternalStoragePublicDirectory is deprecated, I do it in another way. What I have (this method receive bitmap and its name):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        ContentResolver resolver = mContext.getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + IMAGES_FOLDER_NAME);
        Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        OutputStream fos = resolver.openOutputStream(imageUri);
        saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } else {
        String imagesDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).toString() + File.separator + IMAGES_FOLDER_NAME;

        File file = new File(imagesDir);

        if (!file.exists()) {
            file.mkdir();
        }

        File image = new File(
                imagesDir,
                name + ".png"
        );

        final long fileHashCode = image.hashCode();
        Logger.d(TAG, "saveImage, saving image file, hashCode = " + fileHashCode);

        FileOutputStream fos = new FileOutputStream(image);
        saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    }

这对于所有需要的OS版本都可以完美地工作,但是它看起来不准确,我想找到一种更通用的方法.我尝试使用内容值或尝试一些与Q相似的方法,但是它不起作用.我在这里看到了很多问题,但没有一个可以帮助我.

That perfectly works for all needed OS versions, but it looks inaccurate and I'd like to find a more common way. I tried to play around with content values or try some similar way as for Q, but it doesn't work. I've seen many questions here, but neither of them can help me.

问题是如何为低于Q的操作系统优化保存?

The question is how can I optimize saving for OS lower than Q?

推荐答案

我能写的最通用的版本是:

The most generalized version I was able to write is:

private Uri saveImage(Context context, Bitmap bitmap, @NonNull String folderName, @NonNull String fileName) throws IOException
{
    OutputStream fos;
    File imageFile = null;
    Uri imageUri = null;


    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/png");
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM + File.separator + folderName);
        imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        fos = resolver.openOutputStream(imageUri);
    } else {
        String imagesDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).toString() + File.separator + folderName;
        imageFile = new File(imagesDir);
        if (!imageFile.exists()) {
            imageFile.mkdir();
        }
        imageFile = new File(imagesDir, fileName + ".png");
        fos = new FileOutputStream(imageFile);
    }

    boolean saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();

    if (imageFile != null)  // pre Q
    {
        MediaScannerConnection.scanFile(context, new String[]{imageFile.toString()}, null, null);
        imageUri = Uri.fromFile(imageFile);
    }

    return imageUri;
}

如果您找到了更好的方法,请在此处发布,我将其标记为答案.

If you've found a better way, post here, I'll mark it as answer.

这篇关于如何将图像保存到Android Q中的camera文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆