将imageview上传到服务器

时间:2012-02-14 06:13:27

标签: php android image upload android-imageview

public class upload extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
String selectedPath = "";
TextView textTargetUri;
ImageView targetImage;
InputStream is;
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    this.imageView = (ImageView)this.findViewById(R.id.targetimage);
    Button photoButton = (Button) this.findViewById(R.id.takeimage);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });

     Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
     textTargetUri = (TextView)findViewById(R.id.targeturi);
     targetImage = (ImageView)findViewById(R.id.targetimage); // result gambar ditampilkan

     buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
         @Override
         public void onClick(View arg0) {
          Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          startActivityForResult(intent, 0);
         }});
     }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

         if (requestCode == CAMERA_REQUEST) {  
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                imageView.setImageBitmap(photo);
            }

        if (resultCode == RESULT_OK){
            Uri targetUri = data.getData();
            textTargetUri.setText(targetUri.toString());
            Bitmap bitmap;
            try {
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
                targetImage.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

我已经设法从相机显示图片并从文件浏览
但我无法整合上传功能
我已经阅读了其他答案,但它只会出现更多错误 我也把POST PHP的东西弄糊涂了 谁有人可以帮忙?在此之前谢谢

这些是XML

<Button
     android:id="@+id/loadimage"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Open Picture Gallery"
 />
<Button
     android:id="@+id/takeimage"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Take Picture"
 />
<TextView
     android:id="@+id/targeturi"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
 />

<ImageView
    android:id="@+id/targetimage"
    android:layout_width="fill_parent"
    android:layout_height="323sp" />

<Button
    android:id="@+id/uploadimage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Upload Picture" />

2 个答案:

答案 0 :(得分:2)

@VenkataKrishna似乎你的代码让我很烦恼XD。我一直在寻找并找到了新的解决方案

response = httpclient.execute(postRequest);

替换为

response=httpclient.execute(new HttpPost("my url here"));

在try-catch

之前添加它
HttpParams p=new BasicHttpParams();
p.setParameter("parameter", p);
HttpClient httpclient = new DefaultHttpClient(p);

无论如何,谢谢你的帮助:D

答案 1 :(得分:0)

  HttpResponse response=null;

            //uploading image...

            HttpClient httpclient = new DefaultHttpClient();        
            HttpPost postRequest = new HttpPost("your url...");



            try {
                MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);       
                multipartContent.addPart("__VIEWSTATE",new StringBody(""));
                multipartContent.addPart("__EVENTVALIDATION",new StringBody(""));


            FileBody fiebody = new FileBody(new File(your_image_path...));      
                    multipartContent.addPart("fupimage", fiebody);

                postRequest.setEntity(multipartContent);


                response = httpclient.execute(postRequest);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                String sResponse;            
                StringBuilder s = new StringBuilder();
                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);                    
                    System.out.println(sResponse);
                }

                reader.close();            

                System.out.println("Status code:" +response.getStatusLine().getStatusCode());           
                System.out.println("Status code:" +response.getStatusLine().getReasonPhrase());

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

如果您获得response.getStatusLine()。getStatusCode()(状态代码)200,则表示您的图像已成功上传到服务器。