Hammad Nasir Hammad Nasir - 3 years ago 159
Android Question

Two Image paths returned in onActivityResult are getting merged into one resulting in java.io.FileNotFoundException

I'm using android-image-picker library for picking photos from Gallery.

Here's the code:

tapToUploadLabel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImagePicker.create(MainActivity.this)
.returnAfterFirst(false) // set whether pick or camera action should return immediate result or not. For pick image only work on single mode
.imageTitle("Tap to select") // image selection title
.single() // single mode
.multi() // multi mode (default mode)
.limit(3) // max images can be selected (99 by default)
.showCamera(true) // show camera or not (true by default)
.imageDirectory("Camera") // directory name for captured image ("Camera" folder by default)
.enableLog(false) // disabling log
.start(REQUEST_CODE_PICKER); // start image picker activity with request code
}
});


Here's
onActivtyResult()
:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICKER && resultCode == RESULT_OK && data != null) {
ArrayList<Image> images = (ArrayList<Image>) ImagePicker.getImages(data);
printImages(images);
mViewPager.setAdapter(mCustomPagerAdapter);
}
}


Here's
printImages()
:

private void printImages(List<Image> images) {
if (images == null) return;

if (imageArray.size() >= 1) {
imageArray.clear();
}

StringBuilder stringBuffer = new StringBuilder();
for (int i = 0, l = images.size(); i < l; i++) {
stringBuffer.append(images.get(i).getPath()).append("\n");
imageArray.add(Uri.parse(stringBuffer.toString().trim()));
}
}


Now, I'm using this
imageArray
to set Image in
ImageView
like this:

ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
Log.d("imageArrayContains", imageArray.get(position).toString());
imageView.setImageURI(imageArray.get(position));


The problem is that when I'm choosing just 1 image, it is getting set in the ImageView without any error but when I'm choosing 2 or more images, I'm getting this error:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Kwote/1506843168441.jpg
/storage/emulated/0/Pictures/Camera/IMG_20171001_035028145702152.jpg: open failed: ENOENT (No such file or directory)


because 2 filepaths are getting merged into one like this here:
/storage/emulated/0/Pictures/Kwote/1506843168441.jpg
/storage/emulated/0/Pictures/Camera/IMG_20171001_035028145702152.jpg


I'm unable to figure out how can I stop this from getting merged so that I can get all the chosen images loaded in my ImageView.

Please help with this.

Answer Source

You are appending both paths in a same StringBuilder.

Place it inside the for loop.

I believe you will not need a StringBuilder at all. Just use imageArray.add(Uri.parse (images. get(i).getPath()));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download