0

I have built an android app that lets the user click on an image button. Once clicked the user gets the option to open the Gallery or the camera.

I want that the image (chosen from the gallery or taken with the camera) is displayed in the image button. Furthermore I want that the image or its path is stored to an sql database so that when opening up the app once again the user sees the image he set before.

Can anyone give me a code example of doing just that?

user1420042
  • 1,689
  • 9
  • 35
  • 53

2 Answers2

0

There is no short answer to your question, and by posting it all at the same time will learn you less. Anyway there are lots of tutorials on how to do this out there.

I'm guessing you got the gallery and camera code done. Otherwise you may find it in the links below.

Gallery: Get/pick an image from Android's built-in Gallery app programmatically

Camera: Capture Image from Camera and Display in Activity

When you got your Uri from that captured or chosen picture, you can create a bitmap from it:

public Bitmap getBitmapFromUri(Uri uri)
{
    return MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
}

Then you can set the bitmap to the imagebutton using following:

((ImageButton) findViewById(R.id.yourImageButton)).setImageBitmap(bitmap);

One of the links I pasted shows you how to get the path of the image. This path you can either save on your database, or save it with SharedPreferences. You can read about how to use it at the same link. Regarding the database, you know how to get the image path from my answer. Search google for database tutorials. But I think SharedPreferences will be enough depending on the scale of your application.

Good luck!

Community
  • 1
  • 1
Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71
0

Click button and set store image in imageview.

`public class MainActivity extends Activity {

private static final int CAMERA_PIC_REQUEST = 22;

Uri cameraUri;

Button BtnSelectImage;
private ImageView ImgPhoto;
private String Camerapath ;



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

    ImgPhoto = (ImageView) findViewById(R.id.imageView1);

    BtnSelectImage = (Button) findViewById(R.id.button1);
    BtnSelectImage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            try {
                   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                   startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
            }
        }
    });

}


@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
    try {
        switch (requestCode) {
       case CAMERA_PIC_REQUEST:
            if (resultCode == RESULT_OK) {
                try {
                      Bitmap photo = (Bitmap) data.getExtras().get("data"); 

                      ImgPhoto.setImageBitmap(photo);    

                } catch (Exception e) {
                    Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
                }
            }
            break;
          default:
            break;
        }
    } catch (Exception e) {
    }
}

}`

Set this Manifest.

<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>

Guru
  • 186
  • 14