【问题标题】:Saving image to gallery not working since upgrade to android 7升级到 android 7 后无法将图像保存到图库
【发布时间】:2017-03-09 05:37:53
【问题描述】:

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

有谁知道 Android 7.0 发生了什么变化?

【问题讨论】:

  • 在你的课堂上使用这个cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, picUri);
  • 好吧,我会说你的代码。
  • and now the camera doesn't save the image anymore 。相机不保存图像。只有相机应用程序可以这样做。你在用一个吗?您仍然可以使用设备上的相机应用以正常方式拍照吗?
  • the image saved in device gallery 。不会。图像保存在设备存储中。无法将图像保存到图库,因为图库应用程序不是存储位置,而只是一个显示设备上图片的应用程序。

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


【解决方案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;
    }


}

【讨论】:

  • 您必须授予这些权限,希望这对您有所帮助。WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_READ_CONTACTS
  • 一堆不切题的代码。没有人要求这样的代码。是什么让你这么认为?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多