Android 在检索图像时给出空指针
Android gives nullpointer on retrieving images
我正在制作一个应用程序,它使用 AsyncTask 从画廊中挑选 3 张图片。
我的 AsyncTask> 的内容是:
public class ShoppingGallery extends AsyncTask<Void, Void, List<Bitmap>> {
private Activity activity;
private static final String LOG_TAG = ShoppingGallery.class.getSimpleName();
private Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
private String[] projection = {MediaStore.Images.Thumbnails.DATA};
private Cursor imageCursor;
public ShoppingGallery(Activity activity){
this.activity = activity;
imageCursor = activity.getContentResolver().query(uri, projection, null, null, null);
}
@Override
protected List<Bitmap> doInBackground(Void... params) {
List<Bitmap> imgs = new ArrayList<>();
while(imageCursor.moveToNext()){
try {
if(imgs.size() < 3)
imgs.add(MediaStore.Images.Media.getBitmap(activity.getContentResolver(), imageCursor.getNotificationUri()));
} catch (IOException e) {
Log.e(LOG_TAG, "problem with the image loading: " + e);
}
}
return imgs;
}
这对我来说似乎没问题,但是当我 运行 我的程序崩溃并给出以下错误消息时:
08-13 11:14:11.662 22360-22360/com.example.jonas.shoppinglist E/ShoppingContacts:图片执行失败:
java.util.concurrent.ExecutionException:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.net.Uri.getScheme()' on a null object
reference
所以,检测到问题。我的程序抱怨的行是:
imgs.add(MediaStore.Images.Media.getBitmap(activity.getContentResolver(), imageCursor.getNotificationUri()));
来源和解决方法是什么?
你似乎误解了Cursor.getNotificationUri()
方法。
我猜你正在尝试获取返回位图的 uri。
如果是这样,试试这个:
if (imgs.size() < 3) {
String uriStr = imageCursor.getString(0);
Uri uri = null;
if (uriStr == null)
continue;
try {
uri = Uri.parse(uriStr);
} catch (Exception e) {
// log exception
}
if (uri == null)
continue;
Bitmap bm = null;
try {
bm =
MediaStore.Images.Media.getBitmap(activity
.getContentResolver(), uri);
} catch (IOException e) {
// log exception
}
if (bm == null)
continue;
imgs.add(bm);
if (imgs.size() == 3)
break;
}
我正在制作一个应用程序,它使用 AsyncTask 从画廊中挑选 3 张图片。 我的 AsyncTask> 的内容是:
public class ShoppingGallery extends AsyncTask<Void, Void, List<Bitmap>> {
private Activity activity;
private static final String LOG_TAG = ShoppingGallery.class.getSimpleName();
private Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
private String[] projection = {MediaStore.Images.Thumbnails.DATA};
private Cursor imageCursor;
public ShoppingGallery(Activity activity){
this.activity = activity;
imageCursor = activity.getContentResolver().query(uri, projection, null, null, null);
}
@Override
protected List<Bitmap> doInBackground(Void... params) {
List<Bitmap> imgs = new ArrayList<>();
while(imageCursor.moveToNext()){
try {
if(imgs.size() < 3)
imgs.add(MediaStore.Images.Media.getBitmap(activity.getContentResolver(), imageCursor.getNotificationUri()));
} catch (IOException e) {
Log.e(LOG_TAG, "problem with the image loading: " + e);
}
}
return imgs;
}
这对我来说似乎没问题,但是当我 运行 我的程序崩溃并给出以下错误消息时: 08-13 11:14:11.662 22360-22360/com.example.jonas.shoppinglist E/ShoppingContacts:图片执行失败:
java.util.concurrent.ExecutionException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
所以,检测到问题。我的程序抱怨的行是:
imgs.add(MediaStore.Images.Media.getBitmap(activity.getContentResolver(), imageCursor.getNotificationUri()));
来源和解决方法是什么?
你似乎误解了Cursor.getNotificationUri()
方法。
我猜你正在尝试获取返回位图的 uri。
如果是这样,试试这个:
if (imgs.size() < 3) {
String uriStr = imageCursor.getString(0);
Uri uri = null;
if (uriStr == null)
continue;
try {
uri = Uri.parse(uriStr);
} catch (Exception e) {
// log exception
}
if (uri == null)
continue;
Bitmap bm = null;
try {
bm =
MediaStore.Images.Media.getBitmap(activity
.getContentResolver(), uri);
} catch (IOException e) {
// log exception
}
if (bm == null)
continue;
imgs.add(bm);
if (imgs.size() == 3)
break;
}