0

这是代码:

// 以“三星”的方式进行

            // 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)。关于为什么这不起作用的任何想法?

4

1 回答 1

0

您是否在清单文件中授予了相机权限?

于 2012-05-03T10:32:19.323 回答