Android 使用httpurlconnection发送图片

Android 使用httpurlconnection发送图片,android,parameters,httpurlconnection,deprecated,Android,Parameters,Httpurlconnection,Deprecated,嗨,我正在制作一个将图片发送到服务器的应用程序。带有Apache弃用函数的版本可以工作,但我不知道为什么我不能让更新后的解决方案工作。有人知道这里的错误在哪里吗 最新解决方案:它不会在logcat中给出错误,但当我转到服务器时,没有上传任何内容。起初我认为错误在于如何传递参数,但我尝试了几种不同的解决方案,比如使用Uri.builder,使用HashMap和stringBuilder对参数进行编码的方法,像这样传递字符串。。。什么都没用。我需要帮助这真的快把我逼疯了 @Override

嗨,我正在制作一个将图片发送到服务器的应用程序。带有Apache弃用函数的版本可以工作,但我不知道为什么我不能让更新后的解决方案工作。有人知道这里的错误在哪里吗

最新解决方案:它不会在logcat中给出错误,但当我转到服务器时,没有上传任何内容。起初我认为错误在于如何传递参数,但我尝试了几种不同的解决方案,比如使用Uri.builder,使用HashMap和stringBuilder对参数进行编码的方法,像这样传递字符串。。。什么都没用。我需要帮助这真的快把我逼疯了

@Override
    protected Void doInBackground(Void... params) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
        try {
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
        }catch (IOException e){

        }
        HttpURLConnection connection;
       try {
            String urlSt = "http://phoenixcoding.tk/SavePicture.php";
            URL url = new URL(urlSt);
            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");

            /*Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("name", name)
                    .appendQueryParameter("image", encodedImage);

            String query = builder.build().getEncodedQuery();*/

            OutputStream os = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write("name=example&image=" + encodedImage);

            writer.flush();
            writer.close();
            os.close();

           connection.connect();

        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
 }
前一种解决方案:效果很好

@Override
    protected Void doInBackground(Void... params) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
        try {
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
        }catch (IOException e){

        }ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();
        dataToSend.add(new BasicNameValuePair("name", name));
        dataToSend.add(new BasicNameValuePair("image", encodedImage));

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://phoenixcoding.tk/SavePicture.php");

        try{
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;

    }
@覆盖
受保护的Void doInBackground(Void…参数){
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
字符串encodedImage=Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
试一试{
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
}捕获(IOE异常){
}ArrayList dataToSend=新的ArrayList();
添加(新的BasicNameValuePair(“name”,name));
添加(新的BasicNameValuePair(“图像”,encodedImage));
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(“http://phoenixcoding.tk/SavePicture.php");
试一试{
post.setEntity(新的UrlEncodedFormEntity(dataToSend));
客户。执行(post);
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
SavePhoto.php文件:

<?php
$name = $_POST["name"];
$image = $_POST["image"];
$decodedImage = base64_decode("$image");
file_put_contents("pictures/" . $name . ".JPG", $decodedImage);

在php代码中尝试以下操作:

if( isset($_POST["image"]) && !empty($_POST["image"])){
        $profile_pic = '';
        $data= $_POST['image'];

        $data = str_replace('data:image/png;base64,', '', $data);
        $data = str_replace(' ', '+', $data);
        $binary=base64_decode($data);
        header('Content-Type: bitmap; charset=utf-8');
        // Images will be saved under 'www/pictures' folder
        $new_name = $_POST['name'] . '.png';
        $success =file_put_contents('pictures/'.$new_name,$binary);

        $profile_pic = $new_name;

    }

我猜这行代码有问题:-
$decodedImage=base64_decode($image”)你必须这样写,而不是
$decodedImage=base64\u decode($image)

要执行此操作,请执行以下操作:-

<?php 
file_put_contents("post.txt",print_r($_POST,true));
$name = $_POST["name"]; 
.....
?> 

认为:

(如果文件未保存,则存在权限问题。在这种情况下,请创建一个目录“test”,并授予其权限755,即使它不工作,也请将该目录设置为777,然后您的url将被删除)

您将要做的是收集所有传入的
$\u POST
文件,然后您将知道会出现什么POST数据。这将澄清错误在哪里,在android端或php端。如果POST正常,则android代码正常,问题在php代码中

我希望它能帮助你解决这个问题


谢谢大家的回答,我终于成功了。我不知道为什么会这样,但在打开连接后添加InputStream对象后,图片上传正确。我所做的就是添加这些行:

        InputStream is = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder str = new StringBuilder();
        String strLine;
        while ((strLine = reader.readLine()) != null) {
            str.append(strLine + "\n");
        }
        is.close();