问题
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();
}
}
}
}
I already manage to display picture from camera and browse from file
but I can't integrate the Upload function
I have read other answers, but it just get more errors..
I confused with the POST PHP things also
anyone can help? Thanks before
these are the 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" />
回答1:
@VenkataKrishna seems your code is troubling me a lot XD. I keep searching and found a new solution
response = httpclient.execute(postRequest);
replace it with
response=httpclient.execute(new HttpPost("my url here"));
add this before the try-catch
HttpParams p=new BasicHttpParams();
p.setParameter("parameter", p);
HttpClient httpclient = new DefaultHttpClient(p);
anyways, thanks for helping me :D
回答2:
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();
}
If you get response.getStatusLine().getStatusCode() (status code) 200 that means your image is uploaded to server successfully.
来源:https://stackoverflow.com/questions/9272518/upload-imageview-to-server