コードは次のとおりです。
//「Samsung」の方法で実行します
// 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
私は別のソースからこのコードを使用しています。これは、Samsung電話専用にこの修正を考案したものです(Galaxy Nexusを使用しています)。なぜこれが機能しないのかについてのアイデアはありますか?