在sqlite中存储图像

时间:2010-11-29 09:00:20

标签: android android-gallery

我有以下代码:

final Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView parent, View v, int position, long id) {

        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Bitmap b = null;
        b=BitmapFactory.decodeResource(getResources(),*********);

        b.compress(CompressFormat.PNG, 0, outputStream);   
        AlertDialog.Builder builder = new AlertDialog.Builder(Edit.this);
        builder.setTitle("Comfirm");
        builder.setMessage("Do you want to choose this picture?");
        builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                                          image = outputStream.toByteArray();   

                  }                        
              });

如您所见*****需要像android.R.drawble.icon这样的整数。我想在用户点击图片时存储图片。如何在用户点击图片时获取图片?

2 个答案:

答案 0 :(得分:0)

AdapterView.getItemAtPosition(int position)将返回AdapterView中该位置的对象。如果我假设此对象是某种Drawable,那么您可以使用答案hereDrawable转换为Bitmap。获得BitMap后,您可以执行以下操作以获得byte[]

int size = bitmap.getWidth() * bitmap.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();}
byte[] x = out.toByteArray());

然后,您可以将byte[]作为blob存储在sqlite中。

答案 1 :(得分:0)

function createDatabase() 
{
       try{
            if(window.openDatabase){
                var shortName       =   'db_edentiti';
                var version         =   '1.0';
                var displayName     =   'Edentiti Information';
                var maxSize         =   65536; // in bytes
                db                  =   openDatabase(shortName, version, displayName, maxSize);
            }
       }catch(e){
           alert(e);
       }
    }

function executeQuery($query,callback){
    try{
        if(window.openDatabase) {

            db.transaction(
                    function(tx){
                        tx.executeSql($query,[],function(tx,result){
                            if(typeof(callback) == "function"){
                                callback(result);
                            }else{
                                if(callback != undefined){
                                    eval(callback+"(result)");
                                }
                            }

                        },function(tx,error){});
                    });
            return rslt;
        }
    }catch(e){}
}

function createTable()
{
        var sql = 'drop table image';
        executeQuery(sql);
        var sqlC = 'CREATE TABLE image (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, image BLOB )';
        executeQuery(sqlC);
    }
function insertValue()
{
    var img = document.getElementById('image');
    var sql = 'insert into image (name,image) VALUES ("sujeet","'+img+'")';
    executeQuery(sql,function(results){alert(results)});
}

    <input type="button" name='create' onClick="createDatabase()" value='Create Database'>
    <input type="button" name='create' onClick="createTable()" value='create table'>
    <input type="button" name='insert' onClick="insertValue()" value='Insert value'>
    <input type="button" name='select' onClick="showTable()" value='show table'>
    <input type="file" id="image" >
    <div result></div>

有关详细信息和解决方案,请访问以下链接:

http://blog.developeronhire.com/create-sqlite-table-insert-into-sqlite-table/

谢谢