-1

I am not able to send photo to server in Version 6.0.1 and above below versions its working fine. Facing below error.

12-20 16:37:42.888 31885-31885/com.testing E/BitmapFactory: Unable to 
decode stream: java.io.FileNotFoundException: 
/external_files/DCIM/Camera/JPEG_20171220_163728_946425952.jpg: open failed: 
ENOENT (No such file or directory)
12-20 16:37:42.890 31885-31885/com.testing E/AndroidRuntime: FATAL 
EXCEPTION: main
                                                              Process: 
com.testing, PID: 31885

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
                                                                  at 
com.testing.CameraUtils.getBytes(CameraUtils.java:107)
                                                                  at 
com.testing.CameraUtils.saveToInternal(CameraUtils.java:124)

Camera Utils Class code where its showing the error with getBytes method and save to internal method:

public  static String saveToInternal(Context context, Uri tempUri)
{
    OutputStream out = null;
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 10;
    Bitmap bmp= BitmapFactory.decodeFile(tempUri.getPath(),options);
    File destinationInternalImageFile = new File(getOutputInternalMediaFile(context, 1).getPath());
    byte[] bitmapdata=getBytes(bmp);
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
    try {
        destinationInternalImageFile.createNewFile();
        out = new FileOutputStream(destinationInternalImageFile);
        byte[] buf = new byte[1024];
        int len;
        while ((len = bs.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        try {
            if (bs != null) {
                bs.close();
            }
            if (out != null) {
                bs.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Log.e(TAG, "File Size : " + (destinationInternalImageFile.length() / 1024) + "KB");
    return destinationInternalImageFile.getPath();
}

// convert from bitmap to byte array

    public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,50, stream);
    return stream.toByteArray();
}

// convert from byte array to bitmap

    public static Bitmap getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}
Chaudhary Amar
  • 836
  • 8
  • 20

1 Answers1

1

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

This tells it all.

Bitmap bmp= BitmapFactory.decodeFile(tempUri.getPath(),options);

You have no bitmap! bmp==null.

Check before use! Before you call .compress() on it.

But your real mistake is

 Bitmap bmp= BitmapFactory.decodeFile(tempUri.getPath(),options);

You use a wrong path:

/external_files/DCIM/Camera/JPEG_20171220_163728_946425952.jpg

That is a non existing 'path' to begin with.

Change your code to.

InputStream is = getContentResolver().openInputStream(tempUri);
Bitmap bmp= BitmapFactory.decodeStream(is,options);
greenapps
  • 11,154
  • 2
  • 16
  • 19