当字符串来自BLOB时,如何将字符串转换为byteArray?

时间:2017-10-30 16:19:16

标签: java android

我将BLQ从SQite转换为String,我想将该字符串转换为byteArray但不起作用。请帮帮我。

将BLOb转换为ArrayList HashMap上的字符串:

    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> user = new HashMap<String, String>();
            user.put("username" , cursor.getString(9));
            user.put("photo" , cursor.getBlob(10).toString());
            ImageList.add(user);
        } while (cursor.moveToNext());
    }

使用以下代码,我无法将上面的字符串转换为ByteArray,当我运行时,我在ImageView上什么也得不到:

    ArrayList<HashMap<String, String>> photoList;
    photoList = db.getString();
    HashMap<String, String> hashmap= photoList.get(0);

    String photo_name = hashmap.get("photo");
    byte[] byteArray = photo_name.getBytes();

    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    imageview.setImageBitmap(bmp);

1 个答案:

答案 0 :(得分:-1)

public class ConvertStringIntoBytes{

public void convertStringToByteArray() {
    String stringToConvert = "This is test string";
    byte[] theByteArray = stringToConvert.getBytes();
    System.out.println(theByteArray.length);
}

public static void main(String[] args) {
    new Main().convertStringToByteArray();
    }
}

将字符串转换为字节数组的代码。