将照片发布到数据库android时内存不足

时间:2016-02-22 21:32:46

标签: java android imageview bytearray android-volley

我尝试使用齐射将照片发送到我的数据库,CREATE FUNCTION nav.getpathtext_new(path text, locale text DEFAULT 'en') RETURNS json AS转换为byte[]。这是我调用我的凌空方法并从String内的图像中获取String的地方:

ImageView

这是我的凌空方法:

addPic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageView pic_holder = (ImageView) findViewById(R.id.picturedisplay);
            Bitmap bitmap = ((BitmapDrawable)pic_holder.getDrawable()).getBitmap();
            ByteArrayOutputStream stream=new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
            byte[] image=stream.toByteArray();
            String img_str = Base64.encodeToString(image, 0);
            MyLocation myLocation = new MyLocation();
            myLocation.getLocation(getApplicationContext(), locationResult);
            boolean r = myLocation.getLocation(getApplicationContext(),
                    locationResult);
            if (!r) {
                AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
                builder1.setMessage("Please enable your GPS.");
                builder1.setCancelable(false);
                builder1.setPositiveButton(
                        "Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        });

                AlertDialog alert11 = builder1.create();
                alert11.show();
            }
            else {
                savePhoto(Latitude,Longitude,username,picText,img_str);
            }

logcat的:

public void savePhoto(final Double latitude, final Double longitude, final String username, final String description,final String picture ) {
    pDialog.setMessage("Posting picture..");
    pDialog.show();

    request = new StringRequest(Request.Method.POST, SL_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            pDialog.dismiss();
                    Toast.makeText(getApplicationContext(),
                            "Your picture has been posted.", Toast.LENGTH_LONG).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("latitude", Double.toString(latitude));
            hashMap.put("longitude", Double.toString(longitude));
            hashMap.put("image",picture);
            hashMap.put("user", username);
            hashMap.put("description", description);
            return hashMap;
        }
    };
    queue.add(request);
}

1 个答案:

答案 0 :(得分:0)

对Bitmaps要非常小心,因为它们很大并且你得到了这个错误。 在清单文件中的应用程序标记中使用android:largeHeap="true"为我工作,因为你最初有更多的空间,但这仍然无法100%解决问题所以只是尝试最小化内存的使用,创建位图,使用它,并摆脱它,因此试图不将它们存储到列表或地图中。