0

I am trying to develop an android app in which there is an option to upload image from phone memory and then it will display in a list View with some heading given by user.
I have created layout of it but I don't know how to implement this concept.

I used one EditText and a Button for uploading file in LinearLayout and use ListView in another activity to display images

P Sharma
  • 184
  • 9

1 Answers1

1

First you have to get the image from the Phone memory such as `

Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
  Bitmap mBitmap = null;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK)
    {
        Uri chosenImageUri = data.getData();


        mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
        }
}

` You will get the image in the form of Bitmap image. Convert this Bitmap image into the ByteArray[]:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap .compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Then take a column in Database with blob data type and then insert this byte array in that column;

db.execSQL("create table Images(id integer, imageData blob);");

for insert

ContentValues values = new ContentValues();
values.put("id", imageIdValue);
values.put("imageData ", byteArray);
database.insert("Image", null, values);

I hope this will help you.

Bhagwat K
  • 2,859
  • 2
  • 22
  • 31