сохранить изображение в sqlite - PullRequest
1 голос
/ 29 ноября 2010

у меня есть следующий код:

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. Я хочу сохранить изображение, когда пользователи нажимают на изображение. Как я могу получить изображение, когда пользователь нажимает на него?

Ответы [ 2 ]

0 голосов
/ 16 января 2011
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/

Спасибо

0 голосов
/ 29 ноября 2010

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[] в виде большого двоичного объекта в sqlite.

...