自升级到android 7后,将图像保存到图库不能正常工作

时间:2017-03-09 05:37:54

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

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

有谁知道Android 7.0中有哪些变化?

2 个答案:

答案 0 :(得分:1)

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


}

答案 1 :(得分:0)

file:// 不允许再附加Intent,否则会抛出FileUriExposedException,这可能会导致您的应用崩溃立即被调用。