I am unable to upload two separate image in Two Imageview using onActivityResult.
Here when I Select ImageView one and Cope Image then image is set in Imageview.
But when I select Imageview Two and it opens Gallery and When I crop image and press Ok then Imageview one gets replaced it's previous image instead that Imageview two should have the selected image and Imageview one should have same image previously selected
My Activity is
import java.util.Random;
public class UploadBook extends AppCompatActivity {
ImageView iv1,iv2;
private static final int CAMERA_REQUEST_CODE_two=2;
private static final int RESULT_CODE_TWO=2;
private static final int CROP_IMAGE_ACTIVITY_REQUEST_CODE_TWO=2;
private static final int CAMERA_REQUEST_CODE = 1;
Uri filePath = null;
Uri imagePath = null;
public Books b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_book_request);
iv1 = (ImageView) findViewById(R.id.itemImage1);
iv2 = (ImageView) findViewById(R.id.itemImage2);
imageoneButtonclick();
imagetwoButtonclick();
void imageoneButtonclick() {
iv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CropImage.activity(filePath).setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1).start(UploadBook.this);
}
});
}
void imagetwoButtonclick() {
iv2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CropImage.activity(imagePath).setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1).start(UploadBook.this);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
filePath = data.getData();
iv1.setImageURI(filePath);
}
if(requestCode == CAMERA_REQUEST_CODE_two && resultCode == RESULT_CODE_TWO) {
imagePath = data.getData();
iv2.setImageURI( imagePath);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultoneUri = result.getUri();
iv1.setImageURI(resultoneUri);
filePath = resultoneUri;
}
if (resultCode == RESULT_CODE_TWO) {
Uri resultUri = result.getUri();
iv2.setImageURI(resultUri);
imagePath = resultUri;
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
Please Help
Thanks in advance.
Related
I am using ArthurHub's library to crop a image and put it in a ImageButton or ImageView.
Crop Activity starts smoothly but after cropping when I clicks to CROP button, my app reruns and opens MainActivity.
It seems to me that there is no error in the code and also I have checked it 5-6 times.
Thanks!!!
Here is my code...
public class AddActivity extends AppCompatActivity {
private ImageButton add_image_button;
private Uri imageUri;
private String imageUrl;
private String recordPermission = Manifest.permission.RECORD_AUDIO;
private String readExternalStorage = Manifest.permission.READ_EXTERNAL_STORAGE;
private String recordFile;
private int PERMISSION_CODE = 7;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
add_image_button = findViewById(R.id.add_image_button);
CropImage.activity(imageUri).start(AddActivity.this);
add_image_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkReadExternalStoragePermissions();
CropImage.activity(imageUri).start(AddActivity.this);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
imageUri = result.getUri();
add_audio_button.setImageURI(imageUri);
}else {
Toast.makeText(this, "Try again...", Toast.LENGTH_SHORT).show();
startActivity(new Intent(AddActivity.this, MainActivity.class));
finish();
}
}
private boolean checkRecordPermissions() {
if (ActivityCompat.checkSelfPermission(this, recordPermission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{recordPermission}, PERMISSION_CODE);
return false;
}
}
private boolean checkReadExternalStoragePermissions() {
if (ActivityCompat.checkSelfPermission(this, readExternalStorage) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{readExternalStorage}, PERMISSION_CODE);
return false;
}
}
}
implementation 'com.github.Drjacky:ImagePicker:1.8.4'
use this library it is one of best for imagePicker
it will be give functionality like
1)crop
2)zoom
3)rotate
I have found out the error, there was a slight mistake in the code.
I used add_audio_button.setImageUri(imageUri) instead of add_image_button.setImageUri(imageUri); here:
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
imageUri = result.getUri();
add_audio_button.setImageURI(imageUri);
Visitor Details
I have a dialog in Visitor Activity. When I click on the icon, it will redirect to Camera Activity. Then when click Confirm button in Camera Activity, it will send the intent back to previous Activity which is Visitor Activity. The problem is how should I pass the intent result from Camera Activity back to the dialog in Visitor Activity. The intent data should be placed in the Pass No Edit Text. I'm using material dialog without any Fragment that attached to that dialog. It all placed in Visitor Activity. Can someone please help me? Thank you!
private void checkin()
{
final MaterialDialog dialog = new MaterialDialog.Builder(VisitorDetailActivity.this)
.customView(R.layout.sample,false)
.build();
View view = dialog.getCustomView();
final EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
final EditText etVehicleNo = (EditText) view.findViewById(R.id.etVehicleNo);
final ImageView ivScanCode = (ImageView) view.findViewById(R.id.ivScanCode);
final TextView tvCancel = (TextView) view.findViewById(R.id.tvCancel);
final TextView tvSubmit = (TextView) view.findViewById(R.id.tvSubmit);
etVehicleNo.setText(model.getFldVehicleNo());
if(barCode != null)
{
etPassNo.setText(barCode);
}
ivScanCode.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Toast.makeText(VisitorDetailActivity.this, "Bar code scanner", Toast.LENGTH_SHORT).show();
Intent i = new Intent(VisitorDetailActivity.this, ScanCodeActivity.class);
startActivityForResult(i,97);
}
});
tvCancel.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
tvSubmit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(etPassNo.getText().toString().trim().isEmpty())
{
Toast.makeText(VisitorDetailActivity.this, "Pass No is required", Toast.LENGTH_SHORT).show();
return;
}
if(selImage == null)
{
proceedCheckin(etPassNo.getText().toString(), etVehicleNo.getText().toString());
}
else
{
proceedCheckinImage(etPassNo.getText().toString(), etVehicleNo.getText().toString());
}
}
});
dialog.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 98 && resultCode == RESULT_OK)
{
selImage = data.getStringExtra("photo");
Glide.with(VisitorDetailActivity.this).load(selImage).into(iDrivingLicense);
}
else if (requestCode == 99 && resultCode == RESULT_OK)
{
selImage = data.getStringExtra("photo");
Glide.with(VisitorDetailActivity.this).load(selImage).into(iDrivingLicense);
}
else if(requestCode == 97 && resultCode == RESULT_OK)
{
barCode = data.getStringExtra("barCode");
}
}
First you need to take the view object globally out of that checkIn methoed
View view;
view = dialog.getCustomView();
final EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
Now under your onActivityResult method, as i can see you already getting the barcode there, so just initiate the EditText object again on that same simply set the data in it.
barCode = data.getStringExtra("barCode");
EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
etPassNo.setText(barCode);
Let me know if that works.
you can do it using interface
public interface OnBarcodeSelect{
void onBarcodeSelected(String barcode);}
OnBarcodeSelect onBarcodeSelect;
private void checkin()
{
final MaterialDialog dialog = new MaterialDialog.Builder(VisitorDetailActivity.this)
.customView(R.layout.sample,false)
.build();
View view = dialog.getCustomView();
final EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
final EditText etVehicleNo = (EditText) view.findViewById(R.id.etVehicleNo);
final ImageView ivScanCode = (ImageView) view.findViewById(R.id.ivScanCode);
final TextView tvCancel = (TextView) view.findViewById(R.id.tvCancel);
final TextView tvSubmit = (TextView) view.findViewById(R.id.tvSubmit);
etVehicleNo.setText(model.getFldVehicleNo());
if(barCode != null)
{
etPassNo.setText(barCode);
}
ivScanCode.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Toast.makeText(VisitorDetailActivity.this, "Bar code scanner", Toast.LENGTH_SHORT).show();
Intent i = new Intent(VisitorDetailActivity.this, ScanCodeActivity.class);
onBarcodeSelect = new OnBarcodeSelect() {
#Override
public void onBarcodeSelected(String barcode) {
//handle barcode here
etPassNo.setText(barcode);
}
}
startActivityForResult(i,97);
}
});
tvCancel.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
tvSubmit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(etPassNo.getText().toString().trim().isEmpty())
{
Toast.makeText(VisitorDetailActivity.this, "Pass No is required", Toast.LENGTH_SHORT).show();
return;
}
if(selImage == null)
{
proceedCheckin(etPassNo.getText().toString(), etVehicleNo.getText().toString());
}
else
{
proceedCheckinImage(etPassNo.getText().toString(), etVehicleNo.getText().toString());
}
}
});
dialog.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 98 && resultCode == RESULT_OK)
{
selImage = data.getStringExtra("photo");
Glide.with(VisitorDetailActivity.this).load(selImage).into(iDrivingLicense);
}
else if (requestCode == 99 && resultCode == RESULT_OK)
{
selImage = data.getStringExtra("photo");
Glide.with(VisitorDetailActivity.this).load(selImage).into(iDrivingLicense);
}
else if(requestCode == 97 && resultCode == RESULT_OK)
{
barCode = data.getStringExtra("barCode");
if(onBarcodeSelect != null){
onBarcodeSelect.onBarcodeSelected(barCode);
}
}
}
I chose an image to be shown on the screen, then I can choose to upload it to firebase. However the images are 2-4mb, and I want to make them smaller without much loss in quality. Is there anyway I can achieve that?. I want to make the images into like 100kb or less before I upload them to firebase.
I have tried with this compressor https://github.com/zetbaitsu/Compressor, but I had troubles making it work.
Here is my full code:
public class AddNewItem extends AppCompatActivity {
private static final int CHOOSE_IMAGE = 177;
private ImageView imageViewAdd;
private EditText titleEditText, descriptionEditText, priceEditText, cityEditText;
private Button lanButton;
private String titleString, descriptionString, priceString, uploadID;
private Button buttonAdd, buttonRotateImage;
private ProgressBar progressBarImage;
private Uri uriSelectedImage;
private String downloadUriImage;
private FirebaseAuth mAuth;
private int imageRotation=0;
private AlertDialog.Builder mbuilder;
private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef, mDatabaseRefUser;
private StorageTask mUploadTask;
int x = -10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_item);
imageViewAdd = findViewById(R.id.select_image_id);
titleEditText = findViewById(R.id.title_edittext_id);
descriptionEditText = findViewById(R.id.description_edittext_id);
priceEditText = findViewById(R.id.price_edittext_id);
lanButton = findViewById(R.id.lan_button_id);
cityEditText = findViewById(R.id.city_textview_id);
buttonAdd = findViewById(R.id.add_item_button_id);
buttonRotateImage = findViewById(R.id.rotate_image_id);
progressBarImage = findViewById(R.id.progressbar_id);
mAuth = FirebaseAuth.getInstance();
mbuilder = new AlertDialog.Builder(this);
mStorageRef = FirebaseStorage.getInstance().getReference("uploads");
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRefUser = FirebaseDatabase.getInstance().getReference("Users");
}
public void selectImage(View view) {
Intent selectImageIntent = new Intent();
selectImageIntent.setType("image/*");
selectImageIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(selectImageIntent, "select image to show n upload"), CHOOSE_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uriSelectedImage = data.getData();
//new code
Picasso.with(this).load(uriSelectedImage).into(imageViewAdd);
//imageViewAdd.setScaleType(ImageView.ScaleType.FIT_XY); already in xml add_new_item
}
}
private String getFileExtension(Uri uri) {
ContentResolver cR = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cR.getType(uri));
}
//new code
public void uploadFile(View view) {
// the 1st if statement is to prevent spamming of upload button while upload is in progress
if (mUploadTask != null && mUploadTask.isInProgress()) {
Toast.makeText(this, "is in progress", Toast.LENGTH_SHORT).show();
return;
}
if (titleEditText.getText().toString().isEmpty() || titleEditText.getTextSize() < 5) {
titleEditText.setError("Title is short, minimum 5 characters");
titleEditText.requestFocus();
return;
}
if (descriptionEditText.getText().toString().isEmpty() || descriptionEditText.getTextSize() < 20) {
descriptionEditText.setError("Description is short, minimum 20 characters");
descriptionEditText.requestFocus();
return;
}
if(lanButton.getText().toString().isEmpty()){
lanButton.setError("Please Choose District");
lanButton.requestFocus();
return;
}
if (cityEditText.getText().toString().isEmpty() || cityEditText.length() < 3) {
cityEditText.setError("City/Location is short, minimum 3 characters");
cityEditText.requestFocus();
return;
}
if (priceEditText.getText().toString().isEmpty()) {
priceEditText.setError("Price is needed");
priceEditText.requestFocus();
return;
}
else {
if (uriSelectedImage != null) {
StorageReference fileReference = mStorageRef.child(System.currentTimeMillis() +
"." + getFileExtension(uriSelectedImage));
mUploadTask = fileReference.putFile(uriSelectedImage)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Delays the progressbar with 0.5 secs before it shows 0
//user has 0.5 secs to see the 100% bar, code is not necenssary
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
progressBarImage.setProgress(0);
}
}, 500);
Toast.makeText(AddNewItem.this, "Upload succesful", Toast.LENGTH_SHORT).show();
//Upload image is a java class.
uploadID = mDatabaseRef.push().getKey();
UploadImage uploadImage = new UploadImage(titleEditText.getText().toString(),
taskSnapshot.getDownloadUrl().toString(), descriptionEditText.getText().toString(),
cityEditText.getText().toString(), priceEditText.getText().toString(),
imageRotation, lanButton.getText().toString(), mAuth.getCurrentUser().getUid(),
uploadID, mAuth.getCurrentUser().getEmail());
mDatabaseRef.child(uploadID).setValue(uploadImage);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(AddNewItem.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
progressBarImage.setProgress((int) progress);
}
});
} else {
Toast.makeText(this, "no file selected", Toast.LENGTH_SHORT).show();
}
}
}
public void rotateImage(View view) {
//set if condition if you want (for example if image has been set or so.... not necessary though.)
imageViewAdd.setRotation(imageViewAdd.getRotation() + 90);
imageRotation = (int) imageViewAdd.getRotation();
//imageViewAdd.setScaleType(ImageView.ScaleType.FIT_XY); already in xml add_new_item
}
#Override
protected void onStart() {
super.onStart();
if(mAuth.getCurrentUser() == null){
Intent mainActivityIntent = new Intent(this, MainActivity.class);
startActivity(mainActivityIntent);
finish();
}
else {
mDatabaseRefUser.child(mAuth.getCurrentUser().getUid()).child("Online").setValue(true);
}
}
}
I have the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
in the manifest.
How can I solve this?
When I used the Compressor, I changed the ACTION_GET_CONTENT to ACTION_PICK, then I added these lines:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uriSelectedImage = data.getData();
File imageFile = new File(uriSelectedImage.getPath)
compressedImageBitmap = new Compressor(this).compressToBitmap(imageFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageByte = baos.toByteArray();
//new code
Picasso.with(this).load(uriSelectedImage).into(imageViewAdd);
//imageViewAdd.setScaleType(ImageView.ScaleType.FIT_XY); already in xml add_new_item
}
}
in the onActivityResult
and declared private Bitmap compressedImageBitmap also private byte[] imageByte;
I then changed the mUploadTask = fileReference.putFile(uriSelectedImage) to putBytes(imageByte). However I receive several errors such as nullpointer on the compressedImageBitmap. Why is that.
How can I solve this issue?
In this MainActivity java class on Android Application Project I can't replace the original image given by the system with the one different selected from the photo gallery of smartphone.
When I select the one different photo I have always the original image.
public class MainActivity extends Activity implements OnClickListener {
Button uploadButton, btnselectpic;
ImageView imageview;
private ProgressDialog dialog = null;
private String imagepath = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uploadButton = (Button) findViewById(R.id.uploadButton);
btnselectpic = (Button) findViewById(R.id.btnselectpic);
imageview = (ImageView) findViewById(R.id.imageview);
btnselectpic.setOnClickListener(this);
uploadButton.setOnClickListener(this);
#Override
public void onClick(View arg0) {
if (arg0 == btnselectpic) {
selectImage();
} else if (arg0 == uploadButton) {
dialog = ProgressDialog.show(MainActivity.this, "",
"Uploading file...", true);
messageText.setText("uploading started.....");
new Thread(new Runnable() {
public void run() {
}
}).start();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && requestCode == 2 && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
imagepath = getPath(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(imagepath);
imageview.setImageBitmap(bitmap);
messageText.setText("Uploading file path:" + imagepath);
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,
null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
The problem is that your onActivityResult() will never do anything.
You start this method off with:
if (requestCode == 1 && requestCode == 2 && resultCode == RESULT_OK) {
// ...
}
requestCode cannot be both 1 and 2, so this conditional will always be false.
You are likely looking for something more like this:
if ((requestCode == 1 && resultCode == RESULT_OK) ||
(requestCode == 2 && resultCode == RESULT_OK)) {
// ...
}
I am integrating a code for how to capture image and how to get image from gallery.Here is my source code. its working fine for individual but it didn't show the imageview when upload image from gallery. please help me
private static int RESULT_LOAD_IMAGE = 1;
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView,imageView1;
private static final int SELECT_PICTURE = 1;
String selectedPath;
private String selectedImagePath;
Uri selectedImageUri;
//ADDED
private String filemanagerstring;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
//Log.d(TAG, "onShutter'd");
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
protected void onActivityResult1(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
Declare Global Variables just after your class declaration at the top:
private static int RESULT_LOAD_IMAGE = 1;
private static final int PICK_FROM_GALLERY = 2;
Bitmap thumbnail = null;
Call the intent like this: (Yourfunction)
public void YourFunction(){
Intent in = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(in, RESULT_LOAD_IMAGE);
}
Handle the result like this: Declare this outside of your onCreate anywhere in the class.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
thumbnail = (BitmapFactory.decodeFile(picturePath));
Thumbnail is your image, go play with it!