试图从bimap获取ImageUri但最终得到nullpointer异常

时间:2015-05-06 04:13:29

标签: android nullpointerexception uri android-bitmap

我尝试使用此代码从我找到的位图here

获取imageUri

我的代码实际上运行良好,但突然间它开始提供nullpointerexception

这是我的班级

public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);  //LogCat shows error at this line
        Log.e("path", path);
        return Uri.parse(path);
}

这是logcat所说的

05-06 09:37:27.190: E/MediaStore(2725): Failed to insert image
05-06 09:37:27.190: E/MediaStore(2725): java.lang.NullPointerException
05-06 09:37:27.190: E/MediaStore(2725):     at android.os.Parcel.readException(Parcel.java:1327)
05-06 09:37:27.190: E/MediaStore(2725):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
05-06 09:37:27.190: E/MediaStore(2725):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
05-06 09:37:27.190: E/MediaStore(2725):     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:447)
05-06 09:37:27.190: E/MediaStore(2725):     at android.content.ContentResolver.insert(ContentResolver.java:721)
05-06 09:37:27.190: E/MediaStore(2725):     at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:779)
05-06 09:37:27.190: E/MediaStore(2725):     at com.receiver.CustomUploadImages_New.getImageUri(CustomUploadImages_New.java:397)
05-06 09:37:27.190: E/MediaStore(2725):     at com.receiver.CustomUploadImages_New$SaveToDatabase.doInBackground(CustomUploadImages_New.java:245)
05-06 09:37:27.190: E/MediaStore(2725):     at com.receiver.CustomUploadImages_New$SaveToDatabase.doInBackground(CustomUploadImages_New.java:1)
05-06 09:37:27.190: E/MediaStore(2725):     at android.os.AsyncTask$2.call(AsyncTask.java:252)
05-06 09:37:27.190: E/MediaStore(2725):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-06 09:37:27.190: E/MediaStore(2725):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-06 09:37:27.190: E/MediaStore(2725):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
05-06 09:37:27.190: E/MediaStore(2725):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)

1 个答案:

答案 0 :(得分:0)

检查您在上面的方法中传递的位图因为您可能在此方法中传递空位图然后再次运行...并且方法正常工作... 希望它有所帮助,谢谢