Open gallery, select and save address in database - Android

Asked

Viewed 1,154 times

0

I was wondering how I can open the gallery in my android app at runtime, select an image and copy that image to a folder of my application in SD card and save the image path in the database, someone could help me?

1 answer

2

To open your gallery you use this code on your button or something that will click to open the gallery

        Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,
            getString(R.string.get_gallery_picture)),
            Constants.RequestCodes.GET_GALLERY_PICTURE);

no onActivityResult implement this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case Constants.RequestCodes.GET_GALLERY_PICTURE:
        getPictureFromGallery(resultCode, data);
        break;
    }
}

then use this method

private void getPictureFromGallery(int resultStatus, Intent data) {
    if (resultStatus == RESULT_OK && data != null && data.getData() != null) {
        try {
            File image = File.createTempFile(getFilename(), ".jpg",
                    ImageHelper.getStorageDir(this));
            picturePath = "file:" + image.getAbsolutePath();
            ImageDecoderWorker imageDecoder = new ImageDecoderWorker(this,
                    this, image.getName());
            imageDecoder.execute(data.getData());
            loading.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TipUApplication.APP_TAG, "Error: " + e.getMessage());
            showAlert(R.string.default_title_error,
                    R.string.get_gallery_picture_error_message);
        }
    }
}

remembering that these are excerpts of my code you have to change to your maeira

Browser other questions tagged

You are not signed in. Login or sign up in order to post.