1

我有以下代码:

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();   

                  }                        
              });

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

4

2 回答 2

0

AdapterView.getItemAtPosition(int position)将在AdapterView. 如果我假设该对象是某种类型,Drawable那么您可以使用此处的答案将其Drawable转换为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 中。

于 2010-11-29T10:47:59.987 回答
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/

谢谢

于 2011-01-16T11:50:17.153 回答