du31992 2019-08-12 16:04 采纳率: 0%
浏览 1223

如何使用android studio将图像上传到服务器?

我正在尝试从我的Altervista服务器上的目录中上传从图像视图获取的图像。 我看到了很多这样的问题,但是它们使用了HttpClient,HttpPost等,现在已弃用。 我在YouTube上看到了一个教程,并将其调整为我的应用程序,但无法正常工作。 该代码将图像上传到正确的目录中,但是如果我尝试在服务器上打开它,则会出现黑屏。 我将图像下载到我的电脑上,结果更糟。

这是我下载图片时看到的

这里的图片是空的

Php 代码:

    <?php
    $image=$_POST['image'];    
    $decodedImage=base64_decode("$image");
    file_put_contents("/membri/motivationalapp/shareImages/" . "prova" . 
    ".jpg", $decodedImage);
    echo "ok";
    ?>

该图像拍摄于imageView:

    imageView=findViewById(R.id.imageView);
    screenShot=((BitmapDrawable)imageView.getDrawable()).getBitmap();

编码图像的线程:

    new Thread(new Runnable() {
        Background background = new Background(MotAct.this);
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        public void run() {
    screenShot.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
            String encodedImage= Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
            Log.d("debug", encodedImage);
            background.uploadShareImage(encodedImage); //call to AsyncTask
        }
    }).start();

在Asynctask类中上传图片: doInBackground方法:

    final String urlServer = "http://aaa.altervista.org/fileUp/shareImage.php";

                encodedImage=strings[1];
                try {
                    //connecting to server
                    url = new URL(urlServer);
                    httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    OS = httpURLConnection.getOutputStream();
                    bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                    //encode data for post method
                    String dati = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(encodedImage, "UTF-8");
                    bufferedWriter.write(dati);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    OS.close();

                    //read server answer
                    InputStream inputStream = httpURLConnection.getInputStream();
                    bufferedReader= new BufferedReader(new InputStreamReader((inputStream)));
                    stringBuilder= new StringBuilder();

                    while((risposta=bufferedReader.readLine())!=null){
                        stringBuilder.append(risposta+"
");
                    }

                    bufferedReader.close();
                    inputStream.close();
                    inputStream.close();
                    httpURLConnection.disconnect();

                    risposta=stringBuilder.toString().trim();
                    if(risposta.equals("ok")) 
                        return 1;
                    else
                        return 100;

                }catch (MalformedURLException e){
                    e.printStackTrace();
                }
                catch (IOException e1){
                    e1.printStackTrace();
                }

希望大家能够帮助我,谢谢!

  • 写回答

1条回答

  • douzhang7603 2019-08-13 08:02
    关注

    The problem was the php file:

        <?php
            $image=$_POST['image'];    
            $decodedImage=base64_decode($image); //no double quotes
            file_put_contents("/membri/motivationalapp/shareImages/" . "prova" . ".JPEG", $decodedImage); //JPEG instead of jpg
            echo "ok";
        ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建