I let users select an image from gallery, after that I show this in an imageview.
It works on my phone and in nexus 5 emulator but not working in nexus 5x.
Any ideas?
case R.id.imageToUpload:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
break;
and
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
imageToUpload.setImageURI(selectedImage);
}
}
why this code is not working on nexus 5x? (the image is not showing after user selected it)
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
this solved my problem: I added it inside case.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
}
Related
I am trying to upload an image in imageview on the profile fragment. I can upload the image succesfully, but then application very slows down. This is my code:
private void checkAndRequestForPermission() {
if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_FILE);
}
else{
openGallery();
}
}
private void openGallery() {
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(galleryIntent, 1);
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode ==1 && resultCode == RESULT_OK && data!=null && data.getData() !=null) {
getProfilePictureUri = data.getData();
profile_picture.setImageURI(getProfilePictureUri);
}
}
Should I change Uri to Bitmap? Thank you for your help.
Edit: I want to upload high resolution images, If I reduce the size, won't the resolution decrease?
I am trying to choose an image from device's gallery, crop it and then display it in an imageView. After I click on an image in image selector, the app quits with logcat message showing V/FA: Inactivity, disconnecting from the service. I am running a service in background to get data from firestore.
I have also tried using cropping libraries like https://github.com/ArthurHub/Android-Image-Cropper but the behaviour was same.
I also tried the following.
FABfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
mImageUri = data.getData();
imageView.setImageURI(result.getUri());
}
}
Please help to resolve this issue.
Use these methods to choose image from gallery:
public void showGallery() {
if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
loadGallery();
} else {
requestPermissionsSafely(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
private void loadGallery() {
Intent choose = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choose, PICK_IMAGE_GALLERY);
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadGallery();
}
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_GALLERY) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
}
}
}
And:
#TargetApi(Build.VERSION_CODES.M)
public void requestPermissionsSafely(String[] permissions, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions, requestCode);
}
}
#TargetApi(Build.VERSION_CODES.M)
public boolean hasPermission(String permission) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}
how to open only camera and crop using theartofdev dependency .I am using theartofdev dependency 2.7.+ .
Try this
//Declare in Manifest file
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="#style/Base.Theme.AppCompat" />
// Declare global variable
Uri mCropImageUri;
// write below line on the button click
CropImage.startPickImageActivity(this);
// then result will received inside onActivityResult() method
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getImageCallback(requestCode,resultCode,data);
}
public void getImageCallback(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
mCropImageUri = imageUri;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already grunted, can start crop image activity
startCropImageActivity(imageUri);
}
}
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
profileImage.setImageURI(resultUri);
}
}
}
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri).setGuidelines(CropImageView.Guidelines.ON).setCropShape(CropImageView.CropShape.OVAL).start(this);
}
I am facing a strange issue and I hope someone can help me out.
I have a app which the user can select a picture from the gallery and them make some editions.... The problem happens on some devices, the returned URI of the picture loads a thumbnail of the picture.
On my Note 8 (with dynamic focus mode, AKA Portrait mode) it always returns the thumbnail of those pictures. I have no idea why.
My code:
private void openGallery(){
Intent pickPhotoIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
try{
startActivityForResult(pickPhotoIntent, PICK_IMAGE_REQUEST);
}catch (ActivityNotFoundException e){
FirebaseCrash.report(e);
if(mToast != null)
mToast.cancel();
mToast = Toast.makeText(getApplicationContext(),getResources().getText(R.string.no_gallery),Toast.LENGTH_LONG);
mToast.show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_PRO){
if(resultCode == RESULT_OK){
AnalyticsManager.UserEnteredIn(FirebaseAnalytics.getInstance(this),AnalyticsManager.PRICE);
billingProcessor.purchase(Home.this,ImageXY.APESCT_PRO);
}
}
else if(requestCode == PICK_IMAGE_REQUEST){
if(resultCode == RESULT_OK){
if(data != null && data.getData() != null){
onImageReceived(data.getData());
}else
Toast.makeText(getApplicationContext(),"Image Loading Error",Toast.LENGTH_SHORT).show();
}
}
if (!billingProcessor.handleActivityResult(requestCode, resultCode, data))
super.onActivityResult(requestCode, resultCode, data);
}
public void onImageReceived(Uri imageUri) {
Reference.image_uri = imageUri;
Log.d("PICKER","RETURNED");
Intent i = new Intent(getApplicationContext(),ImageEditingActivity.class);
startActivity(i);
}
Thanks in advance, I hope someone can help me.
I am using the Android-Image-Cropper library. It seems to work fine with pictures taken with the camera itself, but when I select images from the gallery that were downloaded from the internet and crop them, the image does not completely cover the ImageButton.
Here is a picture of it not working (downloaded)
Here is one that is working (taken with camera)
Here is my code from the activity:
public void setProfilePicture(View view) {
CropImage.startPickImageActivity(this);
}
#Override
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle result of pick image chooser
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
mCropImageUri = imageUri;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already granted, can start crop image activity
startCropImageActivity(imageUri);
}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
profilepicture.setImageURI(resultUri);
} else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception e = result.getError();
}
}
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// required permissions granted, start crop image activity
startCropImageActivity(mCropImageUri);
} else {
Toast.makeText(this, "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
}
}
}
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
}
Oh wait it figured it out.
I had to set android:scaleType="fitCenter" in my XML and
.setAspectRatio(1, 1)
.setFixAspectRatio(true)
to my startCropImageActivity method.