android - 自升级到 Android 7 后,将图像保存到图库无法正常工作

标签 android image android-gallery android-7.0-nougat

我正在开发一些 Android 应用程序并使用普通相机(不是自定义相机)。该应用程序具有所有必要的权限(相机、写入和读取外部)。直到昨天一切正常:拍摄后,图像保存在设备库中,可以在 ImageView 中显示。我升级到 Android 7.0,现在相机不再保存图像, Activity 结果返回 null (data.getData())。

有人知道 Android 7.0 有什么变化吗?

最佳答案

public class SaveImageAsync extends AsyncTask<Integer, Void, Void> {
    @Override
    protected Void doInBackground(Integer... params) {


        try {


            InputStream in;
            BufferedInputStream buf;
            int position = params[0];

            if (URLUtil.isNetworkUrl(use image here)) {
                in = new URL(use image here
                ).openStream();


                buf = new BufferedInputStream(in);
                Bitmap _bitmapPreScale = BitmapFactory.decodeStream(buf);
                int oldWidth = _bitmapPreScale.getWidth();
                int oldHeight = _bitmapPreScale.getHeight();
                int newWidth = 2592;
                int newHeight = 1936;

                float scaleWidth = ((float) newWidth) / oldWidth;
                float scaleHeight = ((float) newHeight) / oldHeight;

                Matrix matrix = new Matrix();

                matrix.postScale(scaleWidth, scaleHeight);
                Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0, oldWidth, oldHeight, matrix, true);


                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"wave");
                directory.mkdirs();
                directory.mkdir();


                File f = new File(directory, "writeurnamefolder_Gallery" + System.currentTimeMillis() + ".jpg");


                f.createNewFile();

                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());

                fo.close();
            } else if (URLUtil.isFileUrl(use here original image)) {
                MediaStore.Images.Media.insertImage(getContentResolver(), Uri.parse(get your image here.getPath(), "writeurnamefolder_Gallery" + System.currentTimeMillis(), "Gallery Image :");
            }

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

        }

        return null;
    }


}

关于android - 自升级到 Android 7 后,将图像保存到图库无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687567/

相关文章:

java - 在android中自定义图像的大小和尺寸

java - Uri.parse() 在给定的 Url 之前添加 null

java - 更改数据值后如何重新启动Handler?

android - 申请Android Facebook sdk 3.0

html - 具有宽度和高度的图像的解释是响应式的

javascript - 尝试从灯箱画廊中删除讨厌的替代文本

Android 图库水平滚动率

java - 关于Android签名申请的问题

javascript - 设置 Canvas 中图像的大小

带有延迟图像加载适配器的android gallery view "stutters"