這裏使用遊標時是代碼:NullPointerException異常通過返回Android攝像頭意圖
//做的「三星」的方式
// Describe the columns you'd like to have returned.
// Selecting from the Thumbnails location gives you both the
// Thumbnail Image ID, as well as the original image ID
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we
// want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA };
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select
// only
// mini's
MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
// At the moment, this is a bit of a hack, as I'm returning
// ALL images, and just taking the latest one. There is a
// better way to narrow this down I think with a WHERE
// clause which is currently the selection variable
Cursor myCursor = this.managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, null, sort);
/*
CursorLoader loader = new CursorLoader(getApplicationContext(), MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, null, sort);
Cursor myCursor = loader.loadInBackground();
*/
long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";
try {
myCursor.moveToFirst();
**imageId = myCursor
.getLong(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));**
thumbnailImageId = myCursor
.getLong(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor
.getString(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
} finally {
myCursor.close();
}
Uri uriThumbnailImage = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(thumbnailImageId));
// convert to a bytestream -- hack to convert URI to string
上有星號標記的線NPE:
04-19 03:33:23.180: E/AndroidRuntime(29375): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {kaana.app/kaana.app.InEventStartActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
我使用此代碼從另一個來源,專門爲三星手機(我使用Galaxy Nexus)設計此修復程序。任何想法,爲什麼這不工作?
你的'myCursor'沒有任何值,它的大小是零檢查它 – 2012-04-19 07:47:19
我知道,但爲什麼它是0,當我只是成功地拍了一張照片? – maxko87 2012-04-19 15:13:27
你有沒有解決這個問題?我有同樣的問題。 – 2012-12-10 23:53:38