无法压缩回收的位图Android

时间:2017-01-02 22:21:57

标签: android bitmap upload

我正在尝试上传位图图片,这是我的代码

Uri uri = Uri.parse(imagePath);
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);

                    // to add a unique name to the images
                    SimpleDateFormat formatter = new SimpleDateFormat("ymdHs");
                    Date now = new Date();
                    String imageName = formatter.format(now);

                    resizingImage(bitmap, imageName);
for (int a =0; a<imageResizedList.size();a++){

                        HashMap<String, Bitmap> tmpData = new HashMap<String, Bitmap>();
                        tmpData = imageResizedList.get(a);

                        Set<String> key = tmpData.keySet();
                        Iterator it = key.iterator();
                        while (it.hasNext()) {
                            final String imageNameKey = (String)it.next();
                            Bitmap imageBitmapValue = (Bitmap) tmpData.get(imageNameKey);

                            System.out.println("imageNameKey: "+imageNameKey +" & imageBitmapValue: "+imageBitmapValue);

                            final String encodedString = ImageBase64.encode(imageBitmapValue);

                            String url = "http://url/upload.php";
                            StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    pDialog.hide();
                                    Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    pDialog.hide();
                                    Toast.makeText(getActivity(), "Error while uploading image: "+error.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }){
                                @Override
                                protected Map<String, String> getParams() throws AuthFailureError {
                                    Map<String, String> params = new HashMap<>();
                                    params.put("image", encodedString);
                                    params.put("imageName", imageNameKey);
                                    return params;
                                }
                            };

                            // to prevent sending images twice
                            stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                            myCommand.add(stringRequest);

                            it.remove(); // avoids a ConcurrentModificationException

                        }// while end


                    } // for resized bitmaps

调整位图大小:

  private void resizingImage(Bitmap imageBitmap, String imageName){


    imageResizedList.add(getResizedBitmap(imageBitmap, imageName, 1280, 720)); // 1280 x 720
    imageResizedList.add(getResizedBitmap(imageBitmap, imageName, 1366, 768)); // 1366 x 768

}

功能:

public HashMap<String, Bitmap> getResizedBitmap(Bitmap bm, String imageName, int newWidth, int newHeight)
{
    System.out.println("newWidth: "+newWidth+" newHeight: "+newHeight);

    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    HashMap<String, Bitmap> hashMap = new HashMap<String, Bitmap>();
    hashMap.put(imageName+"_"+newHeight+"X"+newHeight+".jpg", resizedBitmap);

    return hashMap;
}

但是它给了我以下例外: java.lang.IllegalStateException:无法压缩回收的位图

在这一行:final String encodedString = ImageBase64.encode(imageBitmapValue);

如何解决此问题?

0 个答案:

没有答案