Elidotnet
Elidotnet

Reputation: 311

Saving image to gallery not working since upgrade to android 7

I am developing some Android app and using the regular camera (not a custom one). The app has all the necessary permissions (camera, write and read external). Until yesterday everything worked fine: after shot, the image saved in device gallery and could be displayed in ImageView. I upgraded to Android 7.0 and now the camera doesn't save the image anymore and the activity results return null (data.getData()).

Does anyone know what's changed in Android 7.0?

Upvotes: 1

Views: 1365

Answers (2)

AnupamChugh
AnupamChugh

Reputation: 1899

file:// is not allowed to attach with Intent anymore or it will throw FileUriExposedException which may cause your app crash immediately called.

Upvotes: 0

sanjeev kumar
sanjeev kumar

Reputation: 551

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


}

Upvotes: 1

Related Questions