我能从字节数组中得到吗?

时间:2013-01-29 09:07:16

标签: sqlite blob

这就是我成功获取应用程序的原生Icon并首先显示它并最终以Blob形式发送到数据库的方式:

Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("application_name");
            builder.setMessage("Vous avez cliqué sur : " + resolveInfo.activityInfo.name);
            Drawable iconApp;
            iconApp=resolveInfo.activityInfo.loadIcon(getPackageManager());

            BitmapDrawable bmd = (BitmapDrawable) iconApp;
            Bitmap bm = bmd.getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] bitmapdata = stream.toByteArray();
            SQLiteAdapter mySQLiteAdapter =new SQLiteAdapter(this.getApplicationContext());
            mySQLiteAdapter.openToWrite();
            String s=resolveInfo.activityInfo.name;
            mySQLiteAdapter.insertphoto(bitmapdata);
            mySQLiteAdapter.insert(s);

**我试图把它拿回来:     **那样:

SQLiteAdapter mySQLiteAdapter =new SQLiteAdapter(getApplicationContext());
                     mySQLiteAdapter.openToRead();
                     byte[] buffer = mySQLiteAdapter.queueAllphoto();
                     mySQLiteAdapter.close();

                     Drawable image = null;
                     image =  new BitmapDrawable(BitmapFactory.decodeByteArray(buffer, 0, buffer.length));
                     Bitmap b= convertByteArrayToBitmap(buffer);

                     //Bitmap bmp = BitmapFactory.decodeByteArray(content,0,content.length);

                     //image =  new BitmapDrawable(BitmapFactory.decodeByteArray(content, 0, content.length));
                     Builder builder = new AlertDialog.Builder(getBaseContext());
                     builder.setTitle("blob");
                     builder.setIcon(image);
                     builder.show();

最终在这里我的mmethode queuAllphoto是SQliteAdapter类的一部分:

public byte[] queueAllphoto(){
        String[] columns = new String[]{KEY_CONTENT_photo};
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE_photo, columns, 
                null, null, null, null, null);
        byte[] result = null; 

        int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT_photo);
        for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
            result = cursor.getBlob(index_CONTENT) ;
        }

        return result;
    }

那个方法返回一个字节数组,我希望通过在AlerDialogue中显示每个blob来处理该字节数组!我正在努力将这个字节数组转换为drawable,因为我想在一个中使用它Builder(AlertDialogue)与方法SetIcon(Drawable)

1 个答案:

答案 0 :(得分:1)

通过改变返回类型(Bitmap而不是字节数组)并添加bitmap = BitmapFactory.decodeByteArray(result,0,result.length)我现在可以使用位图 更改par例如这样的imageview:

 Bitmap bm = null;
                 SQLiteAdapter mySQLiteAdapter =new SQLiteAdapter(getApplicationContext());
                 mySQLiteAdapter.openToRead();
                 bm=mySQLiteAdapter.queueAllphoto();
                 mySQLiteAdapter.close();
                 imageview.setImageBitmap(bm);

你有方法queueAllphoto():

public Bitmap queueAllphoto(){

     Bitmap bitmap;

    String[] columns = new String[]{KEY_CONTENT_photo};
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE_photo, columns, 
            null, null, null, null, null);
    byte[] result = null; 

    int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT_photo);
    for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
        result = cursor.getBlob(index_CONTENT) ;


    }

    bitmap=BitmapFactory.decodeByteArray(result, 0, result.length);
    return bitmap;
}