【发布时间】:2016-12-16 06:41:42
【问题描述】:
我正在开发应用程序,我必须从设备库中一一选择图像。用户可以从Gallary 中一张一张选择任意数量的图片。
用户从图库中仅选择 1 或 2 张图像时可以。但是,问题是当用户选择超过 3 张图片时,应用会加载过多。 当我将字符输入到我的 EditText 时,它也会加载。
在这里,我将图像转换为 Base64,如下所示以将其发布到服务器:
public void JsonArray() {
if (arrayListImages != null) {
jsonItemArray = new JSONArray();
JSONObject jsonObjItemImages = null;
JSONArray jsonArrayItemImages = null;
String strBase64_ItemImage1 = "";
String strBase64_ItemImage2 = "";
String strBase64_ItemImage3 = "";
String strBase64_ItemImage4 = "";
for (int i = 0; i < arrayListImages.size(); i++) {
JSONObject jObj = new JSONObject();
if (arrayListImages.get(i).getStrItemImagePath1() != null) {
strBase64_ItemImage1 = Common.convertToBase64(arrayListImages.get(i).getStrItemImagePath1());
}
if (arrayListImages.get(i).getStrItemImagePath2() != null) {
strBase64_ItemImage2 = Common.convertToBase64(arrayListImages.get(i).getStrItemImagePath2());
}
if (arrayListImages.get(i).getStrItemImagePath3() != null) {
strBase64_ItemImage3 = Common.convertToBase64(arrayListImages.get(i).getStrItemImagePath3());
}
if (arrayListImages.get(i).getStrItemImagePath4() != null) {
strBase64_ItemImage4 = Common.convertToBase64(arrayListImages.get(i).getStrItemImagePath4());
}
jsonObjItemImages = new JSONObject();
jsonArrayItemImages = new JSONArray();
if (strBase64_ItemImage1 != null) {
JSONObject jsonObjectImage1 = new JSONObject();
jsonObjectImage1.put("name", strBase64_ItemImage1);
jsonArrayItemImages.put(jsonObjectImage1);
strBase64_ItemImage1 = null;
}
if (strBase64_ItemImage2 != null) {
JSONObject jsonObjectImage2 = new JSONObject();
jsonObjectImage2.put("name", strBase64_ItemImage2);
jsonArrayItemImages.put(jsonObjectImage2);
strBase64_ItemImage2 = null;
}
if (strBase64_ItemImage3 != null) {
JSONObject jsonObjectImage3 = new JSONObject();
jsonObjectImage3.put("name", strBase64_ItemImage3);
jsonArrayItemImages.put(jsonObjectImage3);
strBase64_ItemImage3 = null;
}
if (strBase64_ItemImage4 != null) {
JSONObject jsonObjectImage4 = new JSONObject();
jsonObjectImage4.put("name", strBase64_ItemImage4);
jsonArrayItemImages.put(jsonObjectImage4);
strBase64_ItemImage4 = null;
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (jsonArrayItemImages != null) {
jsonObjItemImages.put("images", jsonArrayItemImages);
if (jsonObjItemImages != null) {
jObj.put("photo", jsonObjItemImages);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
jsonItemArray.put(jObj);
}
}
从图库中选择图像的功能如下:
private void onSelectFromGalleryResult(Intent data) {
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = myActivity.getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
switch (flagImage) {
case 1:
fileImagePath1 = new File(picturePath);
selectImageView(0, fileImagePath1, 1);
break;
case 2:
fileImagePath2 = new File(picturePath);
selectImageView(0, fileImagePath2, 2);
break;
case 3:
fileImagePath3 = new File(picturePath);
selectImageView(0, fileImagePath3, 3);
break;
case 4:
fileImagePath4 = new File(picturePath);
selectImageView(0, fileImagePath4, 4);
break;
}
}
下面是图片转Base64的函数:
public static String convertToBase64(String path) {
String encodedImage4 = "";
try {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = decodeSampledBitmapFromResource(path, 200, 200);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
encodedImage4 = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return encodedImage4;
}
现在,有人告诉我如何以最佳和有效的方式在 IMAGEVIEW 中加载图像?
或
我的代码有什么需要改进的地方吗? 非常感谢您的建议。
谢谢。
【问题讨论】:
-
不要;不要以这种方式传递图像。传递文件路径并改为打开文件。
-
为画廊使用轻量级库,例如android-arsenal.com/details/1/4395
-
调整图片大小,然后取base64字符串和字节
-
@Qamar 先生,用户一次只能选择一张图片。
-
那个库是可配置的。
标签: android android-studio bitmap imageview base64