Error: Required: Image, Found:Image & Parcelable - java

While trying to get multiple selected images from gallery, there is a compatible issue as shown in screenshot when i use image array list. What could be the solution?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
if (requestCode == ConstantsCustomGallery.REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
//The array list has the image paths of the selected images
ArrayList<Image> images = data.getParcelableArrayListExtra(ConstantsCustomGallery.INTENT_EXTRA_IMAGES);
for (int i = 0; i < images.size(); i++) {
Uri uri = Uri.fromFile(new File(images.get(i).path));
// start play with image uri
}

As i said in comments You need to import the right Image .
import in.myinnos.awesomeimagepicker.models.Image;
instead of
import android.media.Image;

Related

How to croping image with source just from camera with Canhub Android Image Cropper with Java

I want to croping image from other activity to another activity with Canhub Android Image Cropper library. This is my code :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mCropImageUri = result.getOriginalUri();
createImage();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
AppLogger.e(result.getError().getMessage());
}
}
if (requestCode == PermissionCheckUtils.LOCATION_PERMISSION_REQUEST_CODE && resultCode == RESULT_OK) {
getLocation();
}
}
and this when i access the camera :
private void openCropImage() {
Intent intent = CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
with these code, i open camera but source include galery. My question is how to open the croper with source just from camera. I already read the documentation but I'm confused : https://github.com/CanHub/Android-Image-Cropper
It is very simple !!
In Your openCropImage() Function set following code :
Intent intent = CropImage
.activity()
.setImageSource(includeGallery = false, includeCamera = true)
.setGuidelines(CropImageView.Guidelines.ON)
.getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
That's it !! Happy Coding :-)

pdfviewer doesnt open File

myfile.exists() return TRUE, however I couldn't open the pdf:
File myfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ File.separator + fileString ); //fileString=sample.pdf
Log.i("here", fileString + "---"+ myfile.exists());
pdfView = findViewById(R.id.pdfViewer);
pdfView.fromFile(myfile)
.defaultPage(0)
.spacing(2)
.load();
I'm using this library for Pdf view in android.
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
If you wish, I can post all-working snippet code.
(using pdfView.fromUri() works fine but only once. My aim is to pick a .pdf from Intent.ACTION_OPEN_DOCUMENT once and save the uri of it for next usage.
Instead of pdfView.fromFile(), I used pdfView.fromUri(),
pdfView = findViewById(R.id.pdfViewer);
//pdfView.fromFile(myfile)
pdfView.fromUri(Uri.parse(getIntent_uri))
.defaultPage(0)
.spacing(2)
.load();
then after picking file, in onActivityResult() I used takePersistableUriPermission() for make the uri permanent accessible.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable #org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PDF_FILE && resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
special thanks to #CommonsWare

Android Studio - Cannot resolve method 'setText(java.lang.Object)'

I Am Getting An Error in my code That Says Cannot resolve method 'setText(java.lang.Object)'. Does anyone know what causes this error and how i can fix it?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//String str_phonenumber = txt_number.toString();
//str_phonenumber = str_phonenumber.replaceAll("\\D+","");
ArrayList phonenum = new ArrayList(results.get(0).getPhoneNumbers());
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CONTACT_PICKER_REQUEST){
if(resultCode == RESULT_OK) {
results = MultiContactPicker.obtainResult(data);
StringBuilder names = new StringBuilder(results.get(0).getDisplayName());
for (int j=0;j<results.size();j++){
if(j!=0)
names.append(", ").append(results.get(j).getDisplayName());
}
//txt_number.set(phonenum);
txt_number.setText(phonenum.get(0)); //ERROR HERE: Cannot resolve method 'setText(java.lang.Object)'
Log.d("MyTag", results.get(0).getDisplayName());
} else if(resultCode == RESULT_CANCELED){
System.out.println("User closed the picker without selecting items.");
}
}
The reason for index out of bound is because your arraylist is empty but you are trying to get the first element of an empty array!!

Multiple images upload code not working. What am I missing?

I'm developing an Android app in which the user will be asked to upload 2 images from his phone gallery.
My activity has one "Upload" button + 2 ImageViews to show the selected images before proceeding to the next activity.
Everything seems to work fine but both ImageViews are filled with just one of the images I select and I don't know why.
I searched on Google and on this website finding a lot of similar questions, but none of them helped me. Since I'm not an expert I could be missing something stupid and easy, but I'm quite lost at this point and decided to create a post.
Here is my Java code from the activity:
public class UploadActivity extends AppCompatActivity {
//Button and ImageViews have the same names for layout IDs
Button uploadBtn;
ImageView imgOne;
ImageView imgTwo;
public static final int PICK_IMAGE = 100;
public static Uri imgUri1;
public static Uri imgUri2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
uploadBtn = (Button)findViewById(R.id.uploadBtn);
imgOne = (ImageView)findViewById(R.id.imgOne);
imgTwo = (ImageView)findViewById(R.id.imgTwo);
uploadBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() {
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
gallery.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
imgUri1 = data.getData();
imgOne.setImageURI(imgUri1);
imgUri2 = data.getData();
imgTwo.setImageURI(imgUri2);
}
}
}
This code works but shows just one selected image for BOTH ImageViews.
Looks like it's skipping the second image and assigns its ImageView to the first one more time.
Your Problem is this piece of code:
imgUri1 = data.getData();
imgOne.setImageURI(imgUri1);
imgUri2 = data.getData();
imgTwo.setImageURI(imgUri2);
You set imgUri1 AND imgUri2 to data.getData(). So imgUri1 and imgUri2 are exact the same. So you set the same Uri to both imageViews.
Maybe should set data to array data[0]=data1fromimg1 and data[1]=data2fromimg1
in your code like this
<br>
imgUri1 = data[0].getData();<br>
imgOne.setImageURI(imgUri1);<br>
imgUri2 = data[1].getData();<br>
imgTwo.setImageURI(imgUri2);
make sure that receive data
Solved by myself after many tries.
The solution was to use a for like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE && resultCode == RESULT_OK && data.getClipData() != null) {
int count = data.getClipData().getItemCount();
for(int i = 0; i < count; i++) {
Uri screen = data.getClipData().getItemAt(i).getUri();
imgUri1 = data.getClipData().getItemAt(0).getUri();
imgOne.setImageURI(imgUri1);
imgUri2 = data.getClipData().getItemAt(1).getUri();
imgTwo.setImageURI(imgUri2);
}
}
}

Content resolver turns red inside fragment from a public void onActivityResult

I'm trying to access the image gallery and I have these code. Content Resolver turned red in the fragment. I've been trying tweaks, but still it isn't solved.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data!= null && data.getData() != null){
Uri filepath = data.getData();
bitmap = (Bitmap) MediaStore.Images.Media.getBitmap(getContentResolver, filepath);
}
}
Try these variations:
getActivity().getApplicationContext().getContentResolver()
or simply
getActivity().getContentResolver()

Categories

Resources