dongxibeng5324 2018-10-04 01:14
浏览 49

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

I have a code here that uploads image from internal storage to server

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);
        }

but i have multiple errors here.

  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

I dont know whats the problem with my code. My target here is to upload the image inside the internal storage

My Question is how can I upload my Images from internal storage to Server and also how can I check if the loop is done?

by the way here is my 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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测