将图像从内部存储上传到服务器[错误]

时间:2018-10-04 01:14:55

标签: java php android image android-studio

我这里有一个代码,可以将图像从内部存储上传到服务器

Handler handler1 = new Handler();
        Context c = data_syncing.this;
        final File directory = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        final File[] files = directory.listFiles();
        final int x = files.length;
        int i;
        for (i = 0; i < files.length; i++) {
            final int finalI = i;
            final int finalI1 = i;
            final int finalI2 = i;
            handler1.postDelayed(new Runnable() {
                @Override
                public void run() {
                    imgPath = directory + "/" + files[finalI].getName();
                    if (!imgPath.contains(".png")) {
                        fileName = files[finalI].getName();
                        image_view.setImageBitmap(BitmapFactory.decodeFile(imgPath));
                        image_view.buildDrawingCache();
                        Bitmap bitmap = ((BitmapDrawable) image_view.getDrawable()).getBitmap();

                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
                        byte[] image = stream.toByteArray();
                        ConvertImage = Base64.encodeToString(image, Base64.DEFAULT);
                        StringRequest stringRequest = new StringRequest(Method.POST, SYNC_IMAGES,
                                new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                    }
                                }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                            }
                        }) {
                            @Override
                            protected Map<String, String> getParams() throws AuthFailureError {
                                Map<String, String> params = new HashMap<>();
                                params.put("image_tag", fileName.toString());
                                params.put("image_data", ConvertImage.toString());
                                return params;
                            }
                        };
                        VolleySingleton.getInstance(data_syncing.this).addToRequestQueue(stringRequest);
                    }
                }
            }, 1000 * i);
        }

但是我在这里有多个错误。

  1. Failed to allocate a 84652044 byte allocation with 4187552 free bytes and 73MB until OOM
  2. Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

我不知道我的代码有什么问题。我的目标是将图像上传到内部存储器

我的问题是如何将图像从内部存储上传到服务器,以及如何检查循环是否完成?

这是我的php

<?php
  ini_set('display_errors', 1);
  ini_set('display_startup_errors', 1);
  error_reporting(E_ALL);
  include_once('conn.php');

  if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $DefaultId = 0;
    $ImageData = $_POST['image_data'];
    $ImageName = $_POST['image_tag'];
    $ImagePath = "upload/$ImageName";
    $ServerURL = "=AwesomeLinkHere/$ImagePath";
    $InsertSQL = "REPLACE INTO upload (image_path,imagename) values('$ServerURL','$ImageName')";

    if(mysqli_query($con, $InsertSQL)){
      file_put_contents($ImagePath,base64_decode($ImageData));
      echo "Your Image Has Been Uploaded.";
      mysqli_close($conn);
    }else{
      echo "Please Try Again";
    }
  }
?>

0 个答案:

没有答案