应用程序从Gallary中选择图像时负载过重

时间:2016-12-16 06:41:43

标签: android android-studio bitmap imageview base64

我正在开发app,我必须逐个从设备gallary中选择图像。用户可以从Gallary一个接一个地选择他们想要的图像。

当用户仅从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);
        }
    }

从Gallary中选择图像的功能如下:

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中加载图像?

我的代码中有什么需要改进吗? 我将感谢你的建议。

感谢。

2 个答案:

答案 0 :(得分:0)

要加载速度更快,可以使用一个选项

GalleryConfig config = new GalleryConfig.Build()
                        .limitPickPhoto(1)
                        .singlePhoto(true)
                        .hintOfPick("Select image")
                        .filterMimeTypes(new String[]{"image/jpeg"})
                        .build();
GalleryActivity.openActivity(MainActivity.this, reqCode, config);

检查TelegramGallery是否有进一步的实施

答案 1 :(得分:0)

我得到的解决方案如下:

我之前按以下方式加载图片:

Picasso.with(getActivity()).load(fileImagePath1).into(imgviewItemImageHouseHoldDialog1);

但是,现在我添加了.resize(400,400).onlyScaleDown(),如下所示:

Picasso.with(getActivity()).load(fileImagePath1).resize(400,400).onlyScaleDown().into(imgviewItemImageHouseHoldDialog1);

此处,它将使用400 * 400调整图像大小,如果图像大于400 * 400,onlyScaleDown()会调整图像大小。

DONE。

但是,它最终给了我以下错误:

java.lang.OutOfMemoryError: Failed to allocate a 11315274 byte allocation with 6270128 free bytes and 5MB until OOM