首页 > 解决方案 > 如何使用 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+"\n");
                    }

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

希望你们能帮助我。谢谢

标签: phpandroid-studioserverandroid-asynctaskupload

解决方案


问题是php文件:

    <?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";
    ?>

推荐阅读