我在类型ImageView中得到setImageBitmap(Bitmap)不适用于参数(byte [])

时间:2014-12-23 14:20:47

标签: android bitmap android-sqlite

尝试从SQlite数据库加载图像时出现此错误The method setImageBitmap(Bitmap) in the type ImageView is not applicable for the arguments (byte[])

我做的是这个

将图像转换为byte []:

baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();

然后将图像保存到数据库:

contentValues.put(LocationsDB.FIELD_IMAGE, imageData);

然后检索要在imageview中显示的图像:

byte[] img = null;
....
img = arg1.getBlob(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
....
img = markerIcon.setImageBitmap(imageData);

但在setImageBitmap下,我收到了错误

我需要做些什么来解决这个问题? 任何帮助将不胜感激!

已编辑的代码

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    int locationCount = 0;
    double lat=0;
    double lng=0;
    float zoom=0;
    byte[] img = null;
    locationCount = arg1.getCount();
    arg1.moveToFirst();

    for(int i=0;i<locationCount;i++){

        lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
        lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
        zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
        img = arg1.getBlob(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
        thePoint = new LatLng(lat, lng);
        drawMarker(thePoint);
        arg1.moveToNext();
    }

    if(locationCount>0){
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
        bitmap = BitmapFactory.decodeByteArray(imageData , 0, imageData.length);
        img = markerIcon.setImageBitmap(bitmap);
    }
    }

错误

12-23 16:39:38.158: E/AndroidRuntime(8168): FATAL EXCEPTION: main
12-23 16:39:38.158: E/AndroidRuntime(8168): Process: com.example.mainapp, PID: 8168
12-23 16:39:38.158: E/AndroidRuntime(8168): java.lang.NullPointerException
12-23 16:39:38.158: E/AndroidRuntime(8168):     at com.example.mainapp.Map.onLoadFinished(Map.java:338)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at com.example.mainapp.Map.onLoadFinished(Map.java:1)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:4    27)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at  android.support.v4.content.Loader.deliverResult(Loader.java:104)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.os.Looper.loop(Looper.java:136)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at android.app.ActivityThread.main(ActivityThread.java:5586)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at java.lang.reflect.Method.invoke(Method.java:515)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
12-23 16:39:38.158: E/AndroidRuntime(8168):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您需要通过byte[]its decodeByteArray() methodBitmap转换回BitmapFactory

答案 1 :(得分:0)

看起来您正在尝试将byte []数组设置为参数而不是Bitmap值。您可以使用以下代码将byte []转换为Bitmap:

Bitmap bitmap = BitmapFactory.decodeByteArray(imageData , 0, imageData.length);

然后使用此bitmap作为setBitmap()方法的参数。