Advertisement
Guest User

Untitled

a guest
May 5th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. public class Imagen extends ActionBarActivity {
  2. private int SELECT_IMAGE = 237;
  3. private int TAKE_PICTURE = 829;
  4. private int n_touchs=0;
  5. private EditText lblPhoto;
  6. private ImageView imgPhoto;
  7. private RelativeLayout world;
  8. private HttpClient client=new DefaultHttpClient();
  9. private HttpPost post=new HttpPost("192.168.1.138");
  10. private MultipartEntityBuilder builder=MultipartEntityBuilder.create();
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_imagen);
  16. lblPhoto=(EditText)findViewById(R.id.lblPhoto);
  17. imgPhoto=(ImageView)findViewById(R.id.imgPhoto);
  18. world=(RelativeLayout)findViewById(R.id.world);
  19. world.setOnTouchListener(new RelativeLayout.OnTouchListener() {
  20. @Override
  21. public boolean onTouch(View v, MotionEvent event) {
  22. if (n_touchs == 0) {
  23. dialogPhoto();
  24. n_touchs++;
  25. }
  26. return true;
  27. }
  28. });
  29. }
  30.  
  31. private void dialogPhoto(){
  32. try{
  33. final CharSequence[] items = {"Seleccionar de la galería", "Hacer una foto"};
  34.  
  35. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  36. builder.setTitle("Seleccionar una foto");
  37. builder.setItems(items, new DialogInterface.OnClickListener() {
  38. public void onClick(DialogInterface dialog, int item) {
  39. switch(item){
  40. case 0:
  41. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  42. intent.setType("image/*");
  43. startActivityForResult(intent, SELECT_IMAGE);
  44. break;
  45. case 1:
  46. startActivityForResult(new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE), TAKE_PICTURE);
  47. break;
  48. }
  49.  
  50. }
  51. });
  52. AlertDialog alert = builder.create();
  53. alert.show();
  54. } catch(Exception e){}
  55. }
  56.  
  57. @Override
  58. public boolean onCreateOptionsMenu(Menu menu) {
  59. // Inflate the menu; this adds items to the action bar if it is present.
  60. getMenuInflater().inflate(R.menu.menu_imagen, menu);
  61. return true;
  62. }
  63.  
  64. @Override
  65. public boolean onOptionsItemSelected(MenuItem item) {
  66. // Handle action bar item clicks here. The action bar will
  67. // automatically handle clicks on the Home/Up button, so long
  68. // as you specify a parent activity in AndroidManifest.xml.
  69. int id = item.getItemId();
  70.  
  71. //noinspection SimplifiableIfStatement
  72. if (id == R.id.action_settings) {
  73. return true;
  74. }
  75.  
  76. return super.onOptionsItemSelected(item);
  77. }
  78. @Override
  79. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  80. super.onActivityResult(requestCode, resultCode, data);
  81.  
  82. try{
  83. if (requestCode == SELECT_IMAGE) {
  84. if (resultCode == Activity.RESULT_OK) {
  85. Uri selectedImage = data.getData();
  86. lblPhoto.setText("ok");
  87. imgPhoto.setImageURI(selectedImage);
  88. File file_image=new File(selectedImage.toString());
  89. FileBody file_send=new FileBody(file_image);
  90. builder.addPart("file",file_send);
  91. HttpEntity entity=builder.build();
  92. post.setEntity(entity);
  93. }
  94. }
  95. if(requestCode == TAKE_PICTURE) {
  96. if (resultCode == Activity.RESULT_OK) {
  97. Uri selectedImage = data.getData();
  98. lblPhoto.setText("ok");
  99. imgPhoto.setImageURI(selectedImage);
  100. File file_image=new File(selectedImage.toString());
  101. FileBody file_send=new FileBody(file_image);
  102. builder.addPart("file",file_send);
  103. post.setEntity(builder.build());
  104. }
  105. }
  106. } catch(Exception e){}
  107. }
  108.  
  109. 05-05 20:01:41.761: E/AndroidRuntime(11109): FATAL EXCEPTION: main
  110. 05-05 20:01:41.761: E/AndroidRuntime(11109): java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
  111. 05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.ContentType.toString(ContentType.java:153)
  112. 05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:52)
  113. 05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
  114. 05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:230)
  115. 05-05 20:01:41.761: E/AndroidRuntime(11109): at com.example.bryan.imagen.Imagen.onActivityResult(Imagen.java:122)
  116. 05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.Activity.dispatchActivityResult(Activity.java:5563)
  117. 05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.deliverResults(ActivityThread.java:3514)
  118. 05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3561)
  119. 05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.access$1200(ActivityThread.java:168)
  120. 05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1377)
  121. 05-05 20:01:41.761: E/AndroidRuntime(11109): at android.os.Handler.dispatchMessage(Handler.java:99)
  122. 05-05 20:01:41.761: E/AndroidRuntime(11109): at android.os.Looper.loop(Looper.java:137)
  123. 05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.main(ActivityThread.java:5493)
  124. 05-05 20:01:41.761: E/AndroidRuntime(11109): at java.lang.reflect.Method.invokeNative(Native Method)
  125. 05-05 20:01:41.761: E/AndroidRuntime(11109): at java.lang.reflect.Method.invoke(Method.java:525)
  126. 05-05 20:01:41.761: E/AndroidRuntime(11109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
  127. 05-05 20:01:41.761: E/AndroidRuntime(11109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
  128. 05-05 20:01:41.761: E/AndroidRuntime(11109): at dalvik.system.NativeStart.main(Native Method)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement