czane
czane

Reputation: 422

BitmapFactory.decodeStream() : IOException Resetting to invalid mark

I am getting Resetting to invalid mark with BitmapFactory despite the fact the Bitmap I got in return is not null and a valid Jpeg. What I am doing wrong?

Logcat:

01-15 18:54:42.141 12533-12533/com.example.carlo.regional2 E/RecyclerView: No adapter attached; skipping layout
01-15 18:55:01.531 12533-12625/com.example.carlo.regional2 E/NewPostTaskFragment: Error occurred during resize: Resetting to invalid mark
01-15 18:55:01.533 12533-12533/com.example.carlo.regional2 E/NewPostActivity: Couldn't resize bitmap in background task.

Piece of code:

class LoadResizedBitmapTask extends AsyncTask<Uri, Void, Bitmap> {
    private int mMaxDimension;
public LoadResizedBitmapTask(int maxDimension) {
    mMaxDimension = maxDimension;
}

// Decode image in background.
@Override
protected Bitmap doInBackground(Uri... params) {
    Uri uri = params[0];
    //ZC

    try {
    InputStream stream = new BufferedInputStream(
            mApplicationContext.getContentResolver().openInputStream(uri));
    stream.mark(stream.available());
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    stream.reset();
    return bitmap;
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Can't find file to resize: " + e.getMessage());
        FirebaseCrash.report(e);
    } catch (IOException e) {
        Log.e(TAG, "Error occurred during resize: " + e.getMessage());
        FirebaseCrash.report(e);
    }

Upvotes: 0

Views: 1179

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006724

Java's stream APIs leave something to be desired.

In this case, mark() and reset() do not always work, particularly for streams backed by things other than simple files on the filesystem. If you want to use those, call markSupported() first.

The fallback is to request a fresh stream from the ContentResolver.

Upvotes: 4

Related Questions