我正在尝试压缩我在onActivityResult()方法中从库中选择的图像。但是我得到了NullPointerException
代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
//The array list has the image paths of the selected images
ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
for(int i=0;i<images.size();i++){
uri=Uri.parse(images.get(i).path);
if(i==0){
try {
imageStream=getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
Bitmap bmp = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 150, stream);
byte[] byteArray = stream.toByteArray();
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
错误:
03-04 18:04:36.788 8077-8077/com.example.jobinsabu.ohxee E/uri: /storage/extSdCard/DCIM/Camera/20170303_144522.jpg
03-04 18:04:36.788 8077-8077/com.example.jobinsabu.ohxee W/System.err: java.io.FileNotFoundException: No content provider: /storage/extSdCard/DCIM/Camera/20170303_144522.jpg
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1070)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:925)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.content.ContentResolver.openInputStream(ContentResolver.java:650)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at com.example.jobinsabu.ohxee.AddOffers.AddOffers.onActivityResult(AddOffers.java:145)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:6441)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4001)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4048)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.app.ActivityThread.access$1400(ActivityThread.java:177)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1479)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.os.Looper.loop(Looper.java:135)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5910)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at java.lang.reflect.Method.invoke(Native Method)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
03-04 18:04:36.798 8077-8077/com.example.jobinsabu.ohxee D/AndroidRuntime: Shutting down VM
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 android.app.ActivityThread.deliverResults(ActivityThread.java:4005)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4048)
at android.app.ActivityThread.access$1400(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1479)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: 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.example.jobinsabu.ohxee.AddOffers.AddOffers.onActivityResult(AddOffers.java:140)
at android.app.Activity.dispatchActivityResult(Activity.java:6441)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4001)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4048)
at android.app.ActivityThread.access$1400(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1479)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)