Reputation: 556
I wrote some code in android to upload a single bitmap to my server, now I'm trying to upload multiple images to my server, but only the last selected image gets uploaded
Below is how I get the bitmap image in my onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != data) {
if (data.getClipData() != null) {
int count = data.getClipData().getItemCount();
int currentItem = 0;
while (currentItem < count) {
Uri imageUri = data.getClipData().getItemAt(currentItem).getUri();
currentItem = currentItem + 1;
try {
arrayList.add(imageUri);
images.add(MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), imageUri)); // Code to handle multiple images
} catch (Exception e) {
Log.e(TAG, "File select error", e);
}
}
} else if (data.getData() != null) {
final Uri uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), uri);
mCoverImage.setImageBitmap(bitmap);
} catch (Exception e) {
Log.e(TAG, "File select error", e);
}
}
}
}
Below is the code used to upload a single picture to the server
public byte[] getFileDataFromDrawable(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
@Override
protected Map<String, VolleyMultipartRequest.DataPart> getByteData() {
Map<String, VolleyMultipartRequest.DataPart> params = new HashMap<>();
long imageName = System.currentTimeMillis();
params.put("featured_image", new DataPart(imageName + ".png", getFileDataFromDrawable(bitmap)));
return params;
}
The above code works fine
And below is the code used to upload multiple images to the server
public byte[] getFileArrayDataFromDrawable(Bitmap[] bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
for (int i = 0; i < bitmap.length; i++) {
bitmap[i].compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
}
return byteArrayOutputStream.toByteArray();
}
@Override
protected Map<String, VolleyMultipartRequest.DataPart> getByteData() {
Map<String, VolleyMultipartRequest.DataPart> params = new HashMap<>();
Bitmap stockArr[] = new Bitmap[images.size()];
for (int i = 0; i < images.size(); i++) {
stockArr[i] = images.get(i;
}
long imageName2 = System.currentTimeMillis();
params.put("other_images[]", new DataPart(imageName2 + ".png", getFileArrayDataFromDrawable(stockArr)));
return params;
}
But only the last selected image gets uploaded. What am I doing wrong? and how can I do this properly?
Upvotes: 0
Views: 1821
Reputation: 380
call the getBytes method like
there is no need to create Arraylist to collect bitmaps.
ArrayList<Bitmap> images;
// create array to collect images also init in oncreateview method.
images.add(bitmap)
// add images as many you want in onActivityResult
other part of this request visit to upload multiple images this is for single image upload and implement the code blow for multiple
@Override
protected Map<String, VolleyMultipartRequest.DataPart> getByteData() {
Map<String, VolleyMultipartRequest.DataPart> params = new HashMap<>();
for (int i = 0; i < images.size(); i++) {
long imageName2 = System.currentTimeMillis();
params.put("images["+i+"]", new DataPart(imageName2 + ".jpg", getFileDataFromDrawableData(images.get(i))));
}
return params;
}
public byte[] getFileDataFromDrawableData(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(bitmap.getByteCount());
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
bitmap.recycle();
return byteArray;
}
Upvotes: 0
Reputation: 556
I solved this by iterating over the images in a for loop and adding the other_images[i] param in each for loop that executes
for (i in images.indices) {
stockArr[i] = images[i]
try {
val imageName2 = System.currentTimeMillis()
params["other_images[$i]"] = DataPart("$imageName2.png", getFileDataFromDrawable(stockArr[i]!!))
} catch (e: Exception) {
e.printStackTrace()
}
}
return params
Upvotes: 0
Reputation: 5261
compress
only save last bitmap to byteArrayOutputStream
try this code
@Override
protected Map<String, VolleyMultipartRequest.DataPart> getByteData() {
Map<String, List<VolleyMultipartRequest.DataPart>> params = new HashMap<>();
Bitmap stockArr[] = new Bitmap[images.size()];
for (int i = 0; i < images.size(); i++) {
stockArr[i] = images.get(i);
}
ArrayList<VolleyMultipartRequest.DataPart> parts = new ArrayList<VolleyMultipartRequest.DataPart>();
for (Bitmap bitmap : stockArr) {
long imageName2 = System.currentTimeMillis();
parts.add(new DataPart(imageName2 + ".png", getFileArrayDataFromDrawable(bitmap)));
}
params.put("other_images[]", parts);
return params;
}
public byte[] getFileArrayDataFromDrawable(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
Upvotes: 1