MediaStore获取图像高度/宽度

时间:2012-02-22 17:46:05

标签: android mediastore android-mediascanner

我正在查询MediaStore.Images.Media.EXTERNAL_CONTENT_URI,查询工作正常。我能够从每个具有常量的列中获取数据,例如:

currentImage.ImageUrl = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));
            currentImage.Lat = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.LATITUDE));
            currentImage.Long = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.LONGITUDE));

宽度/高度列没有常量(没有MediaStore.Images.Media.WIDTH),因此我尝试使用imagecursor.getLong(imagecursor.getColumnIndex("width"));访问它们。

试图使用getLong,getString,getInt ......没什么 - 总是一样崩溃。

这是堆栈:

02-22 19:38:26.242: E/AndroidRuntime(1607): FATAL EXCEPTION: main
02-22 19:38:26.242: E/AndroidRuntime(1607): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.MEDIA_SCANNER_FINISHED dat=file:///mnt/sdcard flg=0x10 } in com.Activity.android.chat.service.ChatService$ChatServiceReceiver@415b26c0
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.os.Handler.handleCallback(Handler.java:605)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.os.Looper.loop(Looper.java:137)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at dalvik.system.NativeStart.main(Native Method)
02-22 19:38:26.242: E/AndroidRuntime(1607): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.database.CursorWindow.nativeGetLong(Native Method)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.database.CursorWindow.getLong(CursorWindow.java:515)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.database.CursorWindow.getInt(CursorWindow.java:582)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.database.CursorWrapper.getInt(CursorWrapper.java:102)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at com.Activity.android.chat.service.ChatService.getUserImages(ChatService.java:251)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at com.Activity.android.chat.service.ChatService.access$1(ChatService.java:209)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at com.Activity.android.chat.service.ChatService$ChatServiceReceiver.onReceive(ChatService.java:204)
02-22 19:38:26.242: E/AndroidRuntime(1607):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
02-22 19:38:26.242: E/AndroidRuntime(1607):     ... 9 more

感谢任何帮助...不知道我还能尝试什么。

谢谢!

3 个答案:

答案 0 :(得分:9)

解决方案(愚蠢的错误......):

我没有在投影中包含宽度和高度列... 无法理解为什么谷歌不会给他们常数以避免所有这些混乱,但无论如何,解决方案是添加它们。

final String[] columns = {
                MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE, MediaStore.Images.Media.DATE_TAKEN,
                MediaStore.Images.Media.SIZE, MediaStore.Images.Media.LATITUDE, MediaStore.Images.Media.LONGITUDE, "width", "height"
        };

答案 1 :(得分:4)

        try {
                    Bitmap _bitmapPreScale = BitmapFactory.decodeFile(imagePath);

                    int oldWidth = _bitmapPreScale.getWidth();
                    int oldHeight = _bitmapPreScale.getHeight();
    }catch(Exception e){
         e.printstacktrace();
               }

答案 2 :(得分:1)

好像你没有打电话给cursor.moveToFirst() 在访问光标的数据之前。