如何将Imageview图像发布到服务器?

时间:2012-05-17 14:06:24

标签: android

大家好我有一个imageview我正在设置图片,点击该图片发送到服务器请告诉我如何做到这一点

2 个答案:

答案 0 :(得分:0)

使用具有适当参数的Web服务我们可以做到。检查这些链接

1。Android upload image to server using base64

2。Upload image to Server

答案 1 :(得分:0)

添加图书馆Volley

compile 'com.android.volley:volley:1.0.0'

和AndroidManifest.xml中的权限网

  <uses-permission android:name="android.permission.INTERNET"/>   
<uses-permission android:name="android.permission.READ_PROFILE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

拍摄imageview的照片并将其转换为字符串

  ImageView   ivImage1 = (ImageView ) findViewById(R.id.img_add1_send );

  String mmm=getStringImage( ( ( BitmapDrawable ) ivImage1.getDrawable( ) ).getBitmap( ) );

将照片功能转换为字符串

public String getStringImage(Bitmap bm){
ByteArrayOutputStream ba=new ByteArrayOutputStream(  );
bm.compress( Bitmap.CompressFormat.PNG,90,ba );
byte[] by=ba.toByteArray();
String encod= Base64.encodeToString( by,Base64.DEFAULT );
return encod;

}

并将字符串转换为Json

JSONObject requestJsonObject = new JSONObject( );
requestJsonObject.put( "image", mmm );

我们使用以下函数将Json发送到服务器

        JsonObjectRequest jjj = new JsonObjectRequest( Request.Method.POST,
            "http://192.168.16.1/dashbDoard/wDaryounes/GetDShopDDDBojId.php", requestJsonObject,
            new Response.Listener< JSONObject >( ) {
                @Override
                public void onResponse( JSONObject response ) {

                    if ( response == 1 ) {


                        Toast.makeText( getApplication(), "yessss", Toast.LENGTH_SHORT ).show( );
                    } else {

                            Toast.makeText( getApplication(), "no", Toast.LENGTH_SHORT ).show( );

                        }
                    },new Response.ErrorListener( ) {
                        @Override
                        public void onErrorResponse( VolleyError error ) {

                        }
                    } );
                    jjj.setRetryPolicy( new DefaultRetryPolicy( 18000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT ) );
                    Volley.newRequestQueue( G.context ).add( jjj );


            }

在服务器上,很容易将此字符串转换为照片

代码php

$ image = $ _POST ['image'];

    $decodedImage = base64_decode( $image );

    $location = "img/" . $title . "_" . rand( rand(5 , 50) , rand( 500 , 900 ) ) .
                "_" . date("i") . "_" . date("d-m-Y") . ".jpg";

    $resultOfCreatingImage = file_put_contents( $location , $decodedImage );

    if( $resultOfCreatingImage == false )
    {
        $error['error'] = "failure_creating_image";
    }
    else
    {
         $q= "INSERT INTO ads( image) " ."VALUES('$location')
             $res = $connect->prepare($q);
        $res->execute();
        if ($res->rowCount()>0) {
            return 1;
        }else{
            return 0;
        }

    }