1

i have the folloing code :

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

                  }                        
              });

As you see ***** needs to be a int like android.R.drawble.icon. I want to store the picture when users clicks on the picture. How can I get the picture when the user clicks it?

Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
bbkaaka
  • 297
  • 2
  • 5
  • 16

2 Answers2

0

AdapterView.getItemAtPosition(int position) will return the object at that position in the AdapterView. If my assumption that this object is some sort of Drawable then you can use the answer here to convert the Drawable into a Bitmap. Once you have the BitMap you can do the following to get a 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());

You can then store the byte[] as a blob in sqlite.

Community
  • 1
  • 1
dorzey
  • 193
  • 7
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>

For detail info and solution visit the link below:

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

Thank you

Dharmendra
  • 33,296
  • 22
  • 86
  • 129
user565214
  • 31
  • 1