BitmapFactory.decodeFile returns null

I am going to compress the image using the method Bitmap.compress().

But when I get Bitmapusing bitmap = BitmapFactory.decodeFile(), I get the object null, and this method did not rule out any exceptions.

Here is my code

public static File compressImage(String imagePath) throws IOException {

    // Get bitmap
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

    // Get bitmap output stream
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

......

When the code runs on the last line, I get a NullPointerException:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

Then I run my code in debug mode, it turned out that I have an object nullfrom the method BitmapFactory.decodeFile.

The parameter imagePathis

/storage/emulated/0/DCIM/Camera/IMG_20160610_195633.jpg

which looks ok.

, , , . - , ? - ?

+4
2

:

options.inJustDecodeBounds = true;

inJustDecodeBounds:

true, null ( ), out... , .

inJustDecodeBounds Bitmap, , , .

+4

, , USB, . 6.0.1 Android.

"-", . , "settings- > device- > applications- > application manager", , , "" .

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
0

All Articles