2017-04-06 36 views
-5

我想從SQLite數據庫檢索圖像。圖像存儲爲BLOB並嘗試使用數組檢索它。我不知道爲什麼它這樣做。權限都設置正確。嘗試獲取空數組的長度 - 從sqlite數據庫檢索位圖圖像

package com.example.joao_.quizathonegroupteamproject.Activity; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.example.joao_.quizathonegroupteamproject.DatabaseClasses.User; 
import com.example.joao_.quizathonegroupteamproject.R; 

import java.util.ArrayList; 

/** 
* Created by Quoc Nguyen on 13-Dec-16. 
*/ 

public class UserListAdapter extends BaseAdapter { 

    private Context context; 
    private int layout; 
    private ArrayList<User> foodsList; 

    public UserListAdapter(Context context, int layout, ArrayList<User> foodsList) { 
     this.context = context; 
     this.layout = layout; 
     this.foodsList = foodsList; 
    } 

    @Override 
    public int getCount() { 
     return foodsList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return foodsList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    private class ViewHolder{ 
     ImageView imageView; 

    } 

    @Override 
    public View getView(int position, View view, ViewGroup viewGroup) { 

     View row = view; 
     ViewHolder holder = new ViewHolder(); 

     if(row == null){ 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(layout, null); 


      holder.imageView = (ImageView) row.findViewById(R.id.imgFood); 
      row.setTag(holder); 
     } 
     else { 
      holder = (ViewHolder) row.getTag(); 
     } 

     User food = foodsList.get(position); 

     byte[] tblUsersImage = food.getImage(); 
     Bitmap bitmap = BitmapFactory.decodeByteArray(tblUsersImage, 0, tblUsersImage.length); 
     holder.imageView.setImageBitmap(bitmap); 

     return row; 
    } 
} 
+1

'「我不知道爲什麼它這樣做。「」它在做什麼,你是否得到了一個特定的錯誤信息? –

+0

如何保存並從數據庫中讀取位圖? –

+0

你的數組永遠爲空? – Lele

回答

0

您可以檢索BLOB圖像並將其存儲在Byte數組是這樣的:

byte[] array = cursor.getBlob(columnIndex); 

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

然後將其設置爲圖像視圖

holder.imageView.setImageBitmap(bitmap); 
相關問題