如何在android中调整从全宽和全高的gallary中获取的图像?

时间:2016-10-13 09:08:38

标签: android bitmap camera bitmapfactory

这是我调整图像大小的方法: -

 if (resultCode == Activity.RESULT_OK) {
            if (requestCode == GALLERY_ACTIVITY_CODE) {
                Parcelable[] parcelableUris = intent.getParcelableArrayExtra(ImagePickerActivity.EXTRA_IMAGE_URIS);

                if (parcelableUris == null) {
                    Utility.showNotifyMessage(getActivity(), "Please Select an Image");
                } else {
                    // Java doesn't allow array casting, this is a little hack
                    Uri[] uris = new Uri[parcelableUris.length];
                    System.arraycopy(parcelableUris, 0, uris, 0, parcelableUris.length);

                    Uri newImageUploadUrl = Uri.EMPTY;

                    if (uris != null) {
                        for (Uri uri : uris) {
                            Log.i(TAG, " uri: " + uri);
                            newImageUploadUrl = uri;
                        }

                        if (String.valueOf(newImageUploadUrl).equalsIgnoreCase("")) {
                            Utility.showNotifyMessage(getActivity(), "Please Select any Image");
                        } else {
                            long freeSize = 0L;
                            long totalSize = 0L;
                            long usedSize = -1L;
                            try {
                                Runtime info = Runtime.getRuntime();
                                freeSize = info.freeMemory();
                                totalSize = info.totalMemory();
                                usedSize = totalSize - freeSize;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
//                             Log.e("totalSize "+totalSize,"freeSize "+freeSize+"usedSize "+usedSize);


                            Bitmap selectedBitmap = null;

                            try {
                                BitmapFactory.Options options = new BitmapFactory.Options();
                                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                                selectedBitmap = BitmapFactory.decodeFile(String.valueOf(newImageUploadUrl), options);
                            } catch (OutOfMemoryError e) {
                                Log.e("Nithin 0", "" + e.toString());

                                Toast.makeText(getContext(), "Out of Memory,Please free your phone memory", Toast.LENGTH_SHORT).show();

                            }

                            if (selectedBitmap != null) {
                                if (ImageClicked.equalsIgnoreCase("1")) {
                                    try {
                                        usrimage1.setImageBitmap(Bitmap.createScaledBitmap(selectedBitmap, usrimage1.getWidth(), usrimage1.getHeight(), false));
                                        usrimage1.setScaleType(ImageView.ScaleType.FIT_XY);
                                        IsProfilePic = 1;
                                        if (usrimage1.getTag() != new Integer(0))
                                            DeleteImageID = (String) usrimage1.getTag().toString();

                                    } catch (OutOfMemoryError e) {
                                        Log.e("Nithin 1", "" + e.toString());

                                    }
                                } else if (ImageClicked.equalsIgnoreCase("2")) {
                                    try {

                                        IsProfilePic = 2;
                                        usrimage2.setImageBitmap(Bitmap.createScaledBitmap(selectedBitmap, usrimage2.getWidth(), usrimage2.getHeight(), false));
                                        usrimage2.setScaleType(ImageView.ScaleType.FIT_XY);
                                        if (usrimage2.getTag() != new Integer(0))
                                            DeleteImageID = (String) usrimage2.getTag().toString();
                                    } catch (OutOfMemoryError e) {
                                        Log.e("Nithin 2", "" + e.toString());
                                    }
                                } else if (ImageClicked.equalsIgnoreCase("3")) {
                                    try {
                                        IsProfilePic = 3;
                                        usrimage3.setImageBitmap(Bitmap.createScaledBitmap(selectedBitmap, usrimage3.getWidth(), usrimage3.getHeight(), false));
                                        usrimage3.setScaleType(ImageView.ScaleType.FIT_XY);
                                        if (usrimage3.getTag() != new Integer(0))
                                            DeleteImageID = usrimage3.getTag().toString();
                                    } catch (OutOfMemoryError e) {
                                        Log.e("Nithin 3", "" + e.toString());
                                    }

                                } else if (ImageClicked.equalsIgnoreCase("4")) {
                                    try {
                                        IsProfilePic = 4;
                                        usrimage4.setImageBitmap(Bitmap.createScaledBitmap(selectedBitmap, usrimage4.getWidth(), usrimage4.getHeight(), false));
                                        usrimage4.setScaleType(ImageView.ScaleType.FIT_XY);
                                        if (usrimage4.getTag() != new Integer(0))
                                            DeleteImageID = usrimage4.getTag().toString();
                                    } catch (OutOfMemoryError e) {
                                        Log.e("Nithin 4", "" + e.toString());
                                    }
                                } else if (ImageClicked.equalsIgnoreCase("5")) {
                                    try {
                                        IsProfilePic = 5;
                                        usrimage5.setImageBitmap(Bitmap.createScaledBitmap(selectedBitmap, usrimage5.getWidth(), usrimage5.getHeight(), false));
                                        usrimage5.setScaleType(ImageView.ScaleType.FIT_XY);
                                        if (usrimage5.getTag() != new Integer(0))
                                            DeleteImageID = usrimage5.getTag().toString();
                                    } catch (OutOfMemoryError e) {
                                        Log.e("Nithin 5", "" + e.toString());
                                    }
                                }

                                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                                selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

                                byte[] byteArray = byteArrayOutputStream.toByteArray();

                                encoded = resizeBase64Image(Base64.encodeToString(byteArray, Base64.DEFAULT));


    public String resizeBase64Image(String base64image) {
            byte[] encodeByte = Base64.decode(base64image.getBytes(), Base64.DEFAULT);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPurgeable = true;
            Bitmap image = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length, options);


            if (image.getHeight() <= 400 && image.getWidth() <= 400) {
                return base64image;
            }
            image = Bitmap.createScaledBitmap(image, 400, 400, false);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.PNG, 100, baos);

            byte[] b = baos.toByteArray();
            System.gc();
            return Base64.encodeToString(b, Base64.NO_WRAP);

        }

这是我的json解析代码,我希望参数AboutMe作为base 64类型。但它调整了image.i想要完整大小的图像。

  if (generalUtilities.isConnected()) {
                                    pd.setTitle("Please wait...");
                                    pd.setMessage("Saving Pic...");
                                    pd.setCancelable(false);
                                    pd.show();
                                } else {

                                    generalUtilities.showAlertDialog("Hey User !", "Please connect to the internet", "Ok");
                                }


                                StringRequest profilePicRequest = new StringRequest(Request.Method.POST, (getResources().getString(R.string.api_end_point)) + "App1/SaveImage",
                                        new Response.Listener<String>() {
                                            @Override
                                            public void onResponse(String response) {
                                                if (generalUtilities.isConnected()) {
                                                    Log.e("CA", "CA" + response);
                                                    if (pd != null) {
                                                        pd.dismiss();
                                                    }
                                                    try {
                                                        final JSONObject jObject = new JSONObject(response);
                                                        if (jObject.getString("ReplyCode").equals("2")) {
                                                            if (UserProfileImagesID.equalsIgnoreCase("")) {
                                                                UserProfileImagesID = jObject.getString("ProfilePicID");
                                                            } else {
                                                                UserProfileImagesID = UserProfileImagesID + "," + jObject.getString("ProfilePicID");

                                                            }
                                                        }
                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }
                                                } else {
                                                    generalUtilities.showAlertDialog("Error", getResources().getString(R.string.internet_error), "OK");
                                                }
                                            }
                                        },
                                        new Response.ErrorListener() {
                                            @Override
                                            public void onErrorResponse(VolleyError volleyError) {

                                                Toast.makeText(getActivity(), volleyError.getMessage(), Toast.LENGTH_SHORT).show();
                                            }
                                        }) {
                                    @Override
                                    protected Map<String, String> getParams() {
                                        Map<String, String> params = new HashMap<String, String>();
                                        params.put("ID", sharedPreferencesUtilities.getUserId());
                                        params.put("AboutMe", encoded);
                                        params.put("ImageID", String.valueOf(DeleteImageID));
                                        params.put("ProfilePic", String.valueOf(IsProfilePic));
                                        params.put("ImageID1", ImageClicked);



                                        return params;
                                    }
                                };

                                RequestQueue profilePicQueue = Volley.newRequestQueue(getActivity());

                                profilePicQueue.add(profilePicRequest);

                            }


                        }


                    } else {
                        Utility.showNotifyMessage(getActivity(), "Please Select any Image");

                    }

我的问题是我想要获得完整宽度和高度的图像。我不知道相机和位图。任何人都可以告诉我该怎么做?

0 个答案:

没有答案