【问题标题】:How to upload image to Specific URL and fetch it to imageView in android?如何将图像上传到特定 URL 并将其提取到 android 中的 imageView?
【发布时间】:2014-05-07 15:21:40
【问题描述】:

我正在开发一个 android 应用程序,我需要将图片上传到以下 URL。

http://demo1.idevtechnolabs.com/RChatAPI/usrPhotos/

当我将图片上传到此 URL 时,图片应存储在

http://demo1.idevtechnolabs.com/RChatAPI/usrPhotos/YourImage.jpg

用户可以在他/她想要的时候从这个网址看到它。

请我不知道该怎么做。请帮助我或向我发送任何链接。

提前致谢!

【问题讨论】:

标签: android web-services url upload imageview


【解决方案1】:

按照给定的步骤:

1)您必须将图像转换为位图,

2)将位图转换为Base64字符串,

3)将此字符串发送到服务器。

要表示此图像:

1) 将此Base64转换为位图,并将此位图设置为imageview。

点击给定的链接

http://www.codeproject.com/Questions/464629/Pass-byte-as-parameter-to-JSON-service-from-Androi

【讨论】:

  • 图片发送到服务器或特定url时是否需要转换成Bitmap格式?
  • 你可以使用FTP等其他程序。我觉得这个程序很简单。
  • Acctully 我得到了 HTPP 响应是:不允许的方法:405
  • 我认为出现了 http 连接问题,或者您的论点没有根据 web 服务很好地记录。
【解决方案2】:

试试这个(使用 volley 上传图片)

我只是从android端编写上传方法的sn-ps和php文件来接收文件(以字符串的形式)

ANDROID/JAVA 代码

// getStringImage method will be called inside uploadImage 

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

private void uploadImage(){
    //Showing the progress dialog
    final ProgressDialog loading = ProgressDialog.show(this, "Uploading...", "Please wait...", false, false);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    //Disimissing the progress dialog
                    loading.dismiss();
                    //Showing toast message of the response
                    Toast.makeText(MainActivity.this, "uploaded" , Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    //Dismissing the progress dialog
                    loading.dismiss();

                    //Showing toast
                    Toast.makeText(MainActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            //Converting Bitmap to String
            String image = getStringImage(bitmap);

            //Getting Image Name
            String name = editTextName.getText().toString().trim();

            //Creating parameters
            Map<String,String> params = new Hashtable<String, String>();

            //Adding parameters
            params.put(KEY_IMAGE, image);
            params.put(KEY_NAME, "name");

            //returning parameters
            return params;
        }
    };

    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

PHP端img.php

<?php

    $host="localhost"; //replace with database hostname 
    $username="root"; //replace with database username 
    $password=""; //replace with database password 
    $db_name="mydb"; //replace with database name

    $con=mysql_connect($host,$username,$password);
    $db=mysql_select_db($db_name);

    $name = $_REQUEST['name']; //image name
    $image = $_REQUEST['image']; //image in string format
    $user=$_REQUEST['User_ID'];

    $decodedImage = base64_decode($image);
    $image_file=time().rand(1111,9999);
    $name=$name.$image_file;

    $base_path='/var/www/html/uploads/';
    file_put_contents($base_path.$name.".jpg", $decodedImage);

    mysql_query("INSERT into `image`(`img`,`User_ID`,`date`) values ('".$image_file.".jpg','$user',now() )");
    echo mysql_insert_id();

?>

这是一个迟到的回复,但我希望它可以帮助其他人:) 如果您在集成上述代码时遇到任何问题,请回复我。

【讨论】:

    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2017-11-24
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多