I wanted to know how I can open the gallery in my android application at runtime, select an image and copy that image to a folder of my application on the SD card and save the image path in the database, could someone help me ?
I wanted to know how I can open the gallery in my android application at runtime, select an image and copy that image to a folder of my application on the SD card and save the image path in the database, could someone help me ?
To open your gallery you use this code in your button or something of the type 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);
in 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 snippets of my code you have to change to your own