ajay
ajay

Reputation: 11

Take a photo using camera and store it in sqlite database

I am new to android. I created an application for taking a photo using android phone and saving that photo in a Sqlite database. But this code is not running. I am unable to find the error in the program. Please help me.

public class MainActivity extends Activity {

    private static final int CAMERA_REQUEST = 0; 
    public ImageView imageView; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView =(ImageView) findViewById(R.id.image);
         Button B = (Button) this.findViewById(R.id.camera);

         B.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            }

         });

    }


     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
          ImageHelper help= new ImageHelper(this);
            if (requestCode == CAMERA_REQUEST) {  
                Bitmap photo = (Bitmap) data.getExtras().get("data");   
                imageView.setImageBitmap(photo);  
                ByteArrayOutputStream out=new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.PNG,100,out);
                byte ByteArr[]=out.toByteArray();
                help.insert(ByteArr);
            }
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

} 

Database code

public class ImageHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME="image";
private static final int DATABASE_VERSION=1;

public ImageHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

public Cursor getAll() {                               
    return(getReadableDatabase().rawQuery("SELECT imageblob FROM Image",null));
}      

public void insert(byte Byte[]){
     ContentValues cv=new ContentValues();
     cv.put("imageblob",Byte);
    Log.e("inserted","inserted");
    getWritableDatabase().insert("image","imageblob",cv);

}


public byte[] getImage(Cursor c)
{
   return(c.getBlob(1));
}
}

Upvotes: 0

Views: 5556

Answers (1)

David N
David N

Reputation: 519

It would be easier to say with LogCat... What is the error ?

It's not the question but i advice you to store image URI as String instead of a byte array in sqlite database.

To get URI from byte array :

        String path = Images.Media.insertImage(getContentResolver(), ImageInByte,
                "title", null);

        Uri imageUri = Uri.parse(path);

        String uriString = uriMyLogo.toString() ;

And to get Image from URI

                Uri imageUri = Uri.parse(uriString);

                Bitmap image = null;
                try {
                    image = Media.getBitmap(this.getContentResolver(), imageUri );
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

Upvotes: 1

Related Questions