如何将图像上传到特定URL并将其提取到android中的imageView?

时间:2014-03-28 09:00:37

标签: android web-services url upload imageview

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

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

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

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

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

我不知道该为此做些什么。请帮帮我或给我任何链接。

提前致谢!

2 个答案:

答案 0 :(得分:3)

按照给定的步骤:

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

2)将位图转换为Base64 String,

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

表示此图片:

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

按照指定的链接

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

答案 1 :(得分:3)

试试这个(使用凌空上传图片)

我只是从android端和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();

?>

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