Question

I'm having an issue whereby when I write a bitmap to disk, it gets written to disk, however it gets written as a miniscule image (3kb or less in filesize).

I have checked that the source image is indeed the correct dimensions, however the output image seems shrunk despite configuring the bitmap options to not scale.

@Override
protected Void doInBackground(PPImage... params) {
    String filename = "pp_" + position + ".jpg";
    File externalStorageDirectory = Environment.getExternalStorageDirectory();
    final File destination = new File(externalStorageDirectory, filename);

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 16;
    opts.inPurgeable = true;
    opts.inScaled = false;

    decode(opts, Uri.parse(params[0].getUri()), getActivity(), new OnBitmapDecodedListener() {
        @Override
        public void onDecoded(Bitmap bitmap) {
            try {
                FileOutputStream out = new FileOutputStream(destination, false);
                writeImageToFileTask.this.holder.pathToImage = destination.getAbsolutePath();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();

                MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), destination.getAbsolutePath(), destination.getName(), destination.getName());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    return null;
}

private void decode(BitmapFactory.Options options, Uri mUri, Context mContext, OnBitmapDecodedListener listener) {
    try {
        InputStream inputStream;
        if (mUri.getScheme().startsWith("http") || mUri.getScheme().startsWith("https")) {
            inputStream = new URL(mUri.toString()).openStream();
        } else {
            inputStream = mContext.getContentResolver().openInputStream(mUri);
        }

        Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);

        listener.onDecoded(bitmap);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

How do I ensure that the image being written to file is the same dimensions as the original source image?

Était-ce utile?

La solution

You have specified sample size in your code, which will result in resizing:

opts.inSampleSize = 16;

Just remove this line, and the dimension of the output image should be the same.

About the usage of inSampleSize, according to official doc:

For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top