如何拍照并保存在 Android Q Scoped Storage 中?

问题描述

我已经用这个 https://github.com/memfis19/Annca 在我的应用程序中拍照,但它不适用于 android Q,我不知道如何处理将照片保存到我的文件

这个函数获取OutputMediaFile

https://github.com/memfis19/Annca/blob/master/annca/src/main/java/io/github/memfis19/annca/internal/utils/CameraHelper.java

public static File getoutputMediaFile(Context context,@AnncaConfiguration.MediaAction int mediaAction) {
 
        String filepath = Environment.getExternalStorageDirectory().getPath();
        File appFolder = new File(filepath,context.getString(R.string.folder_app_name));
        if (!appFolder.exists()) {
            appFolder.mkdirs();
        }

        File mediaStorageDir = new File(appFolder.getPath(),"Images");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(TAG,"Failed to create directory.");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date());
        File mediaFile;
        if (mediaAction == AnncaConfiguration.MEDIA_ACTION_PHOTO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_" + timeStamp + ".jpg");
        } else if (mediaAction == AnncaConfiguration.MEDIA_ACTION_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }
        return mediaFile;

}

这是保存文件

https://github.com/memfis19/Annca/blob/master/annca/src/main/java/io/github/memfis19/annca/internal/utils/ImageSaver.java

public class ImageSaver implements Runnable {
private final static String TAG = "ImageSaver";

private final Image image;
private final File file;
private ImageSaverCallback imageSaverCallback;

public interface ImageSaverCallback {
    void onSuccessFinish();

    void onError();
}

public ImageSaver(Image image,File file,ImageSaverCallback imageSaverCallback) {
    this.image = image;
    this.file = file;
    this.imageSaverCallback = imageSaverCallback;
}

@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void run() {
    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
    FileOutputStream output = null;
    try {
        output = new FileOutputStream(file);
        output.write(bytes);
        imageSaverCallback.onSuccessFinish();
    } catch (IOException ignore) {
        Log.e(TAG,"Can't save the image file.");
        imageSaverCallback.onError();
    } finally {
        image.close();
        if (null != output) {
            try {
                output.close();
            } catch (IOException e) {
                Log.e(TAG,"Can't release image or close the output stream.");
            }
        }
    }
}

}

我应该怎么做才能通过 Scoped Storage 支持 android Q?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)