user1420042
user1420042

Reputation: 1709

How to store an image in an image view like in the contact manager?

Everyone knows how the contact manager looks like. In the upper corner you have this image view where you can click on. This enables you to choose an image from your gallery.

What I am looking for is how I can implement this feature in my app. I already have set up an image button where you can click on. This brings you to the gallery.

The next thing would be to set up the onActivityResult method, maybe a database to store the image and a way to retrieve the image back so that it can be displayed in the image button.

Please, I really want to know how to built this but don't know how to start. Can someone post some sample code of the mentioned steps? You would be my hero!

Upvotes: 0

Views: 163

Answers (1)

Amokrane Chentir
Amokrane Chentir

Reputation: 30395

From your question, it looks like you are struggling the most with the onActivityResult part.

It should be something like this:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if(requestCode == 0 && data != null && data.getData() != null) {
       Uri uri = data.getData();
       if(uri != null) {
          Cursor cursor = getContentResolver().query(uri, new String[] {   
                                   android.provider.MediaStore.Images.ImageColumns.DATA}, 
                                   null, null, null);
                cursor.moveToFirst();
                String imageFilePath = cursor.getString(0);             
                cursor.close();

                if(imageFilePath != null) {
                        // Do whatever you want like decode it into a Bitmap
                        Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath);
                        // Or.. store it somewhere in your local db  
                    }
       }
   }
}

After you have retrieved the file path for your image, you are free to do whatever you want. As you said, you can obviously store that in a local database in your user table for instance.

Upvotes: 2

Related Questions