0

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!

4

1 に答える 1

2

あなたの質問から、あなたはその部分で最も苦労しているように見えますonActivityResult

次のようになります。

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  
                    }
       }
   }
}

画像のファイルパスを取得したら、自由に好きなことを行うことができます。あなたが言ったように、あなたは明らかにそれを例えばあなたのユーザーテーブルのローカルデータベースに保存することができます。

于 2012-05-30T14:09:01.290 に答える