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

如何使用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 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";
        ?>
    
    点赞 评论 复制链接分享

相关推荐