mardi 20 octobre 2015

How to save captured image path in sqlite database and retrieve it

I'm still new to this Android Studio. I've been searching for coding that suitable with my current apps. But I cannot find one that I can understand or relate to my apps.

I want to create a camera button that capture image and the path of the image will be save inside sqlite database. In the same activity, I want to show the image in form of listview.

Right now I only able to call camera function inside MainActivity.

    public void btnAmbilGambar (View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
        OutputFileUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, OutputFileUri);
        startActivityForResult(intent, takePic);
    }

    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        if (requestCode == takePic && resultCode==RESULT_OK){
            Message.message(this, OutputFileUri.toString());
        }

    }

The below coding is the method to insert image inside my database.

public long insert(Bitmap img ) {

        SQLiteDatabase db=helper.getWritableDatabase();
        byte[] data = getBitmapAsByteArray(img); // this is a function
        ContentValues value=new ContentValues();
        value.put("pic",data);

        long a= db.insert("mypic", null, value);

        return a;
    }

    public static byte[] getBitmapAsByteArray(Bitmap bitmap)
    {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
        return outputStream.toByteArray();
    }

When I run the apps, there are no error but I don't know how to save the image inside the database. I need coding of how to call the insert() method and then how to display the image in form of listView. If someone can show me a sample of coding which can complete my coding will be much appreciated. Thank you.

Aucun commentaire:

Enregistrer un commentaire