使用Android Studio

时间:2017-09-10 07:56:55

标签: android

我是Android Studio新手。我使用Retrofit作为服务器请求。我想在数据库中发送图像,并希望在同一图像中显示。我该怎么办?

1 个答案:

答案 0 :(得分:0)

  1. 将图像转换为字节数组并使用db query

    应用它
    public static byte[] getBytes(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, stream);
        return stream.toByteArray();
    }
    
  2. 从db获取字节数组后,您应该将其转换为图像

    public static Bitmap getImage(byte[] image) {
        return BitmapFactory.decodeByteArray(image, 0, image.length);
    }
    
  3. 创建对象的类并声明列表视图的自定义适配器:

    //your object's class
    public class Book {
        String mName;
        long mPrice;
        Bitmap mPhoto;
    }
    
    //custom adapter
    public class CustomAdapter extends ArrayAdapter<Book> {
        public CustomAdapter(Context context, List<Book> books) {
            super(context, 0, books);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if(row == null) {
                // inflate row layout and assign to 'row'
            }
            final thisBook = getItem(position);
            final ImageView photo = row.findViewById(R.id.photo);
            photo.setImageBitmap(thisBook.mPhoto);
    
            return row;
        }
    }
    
  4. 另请尝试tutorial