'Uploading images from different imageViews to the server
I'm trying to upload two images from two ImageViews to the server but I'm challenged. When I try with one ImageView it works but two ImageViews, it doesn't.
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:scaleType="centerCrop"
android:padding="5dp"
android:src="@drawable/id_card"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:scaleType="centerCrop"
android:padding="5dp"
android:src="@drawable/ref_card"/>
My requestCode looks like this:
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_ONE);
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_TWO);
My onActivityResult code looks like this:
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (resultCode == Activity.RESULT_OK) {
if(requestCode == IMAGE_ONE)
imageView1.setImageBitmap(imageOne);
else if(requestCode == IMAGE_TWO)
imageView2.setImageBitmap(imageTwo);
}
Then,
BitmapDrawable drawable1 = (BitmapDrawable) imageView1.getDrawable();
BitmapDrawable drawable2 = (BitmapDrawable) imageView2.getDrawable();
Bitmap idCardBitmap = drawable1.getBitmap();
Bitmap refIdBitmap = drawable2.getBitmap();
final String image1 = getStringImage(idCardBitmap);
final String image2 = getStringImage(refIdBitmap);
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
To upload,
ApiInterface api = ApiClient.getApiService();
Call<ResponseBody> call = api.registerDetails(image1,image2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
