radityakurnianto

MainActivity

Jun 15th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. package com.uploadimage;
  2.  
  3. import java.io.File;
  4. import java.io.ByteArrayOutputStream;
  5. import java.util.ArrayList;
  6.  
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.message.BasicNameValuePair;
  9. import org.json.JSONObject;
  10.  
  11. import android.os.Bundle;
  12. import android.os.AsyncTask;
  13. import android.app.Activity;
  14. import android.app.ProgressDialog;
  15. import android.view.Menu;
  16. import android.view.View;
  17. import android.content.Intent;
  18. import android.graphics.Bitmap;
  19. import android.graphics.Bitmap.CompressFormat;
  20. import android.graphics.BitmapFactory;
  21. import android.net.Uri;
  22. import android.provider.MediaStore;
  23. import android.util.Log;
  24. import android.widget.Button;
  25. import android.widget.EditText;
  26. import android.widget.ImageView;
  27. import android.database.Cursor;
  28.  
  29. public class MainActivity extends Activity {
  30.     ImageView image;
  31.     Button upload, browse;
  32.     EditText caption, byte_array;
  33.     Intent intent;
  34.     Uri selected_image;
  35.     File file;
  36.     Cursor cursor;
  37.     ProgressDialog pDialog;
  38.     JSONObject json;
  39.     JSONParser jsonParser = new JSONParser();
  40.     String picturePath, title;
  41.    
  42.     private static int RESULT_LOAD_IMAGE = 1;
  43.     private static final String url = "http://192.168.1.8/psn_asset_oracle/images.php";
  44.    
  45.     @Override
  46.     protected void onCreate(Bundle savedInstanceState) {
  47.         super.onCreate(savedInstanceState);
  48.         setContentView(R.layout.activity_main);
  49.         image = (ImageView)findViewById(R.id.image);
  50.         upload = (Button)findViewById(R.id.upload);
  51.         browse = (Button)findViewById(R.id.browse);
  52.         caption = (EditText)findViewById(R.id.caption);
  53.         byte_array = (EditText)findViewById(R.id.byte_array);
  54.        
  55.         browse.setOnClickListener(new View.OnClickListener()
  56.         {
  57.             @Override
  58.             public void onClick(View v)
  59.             {
  60.                 intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  61.                 startActivityForResult(intent, RESULT_LOAD_IMAGE);
  62.             }
  63.         });
  64.        
  65.         upload.setOnClickListener(new View.OnClickListener()
  66.         {
  67.             @Override
  68.             public void onClick(View v)
  69.             {
  70.                 title = caption.getText().toString();
  71.                 new upload_image().execute();
  72.             }
  73.         });
  74.     }
  75.    
  76.     class upload_image extends AsyncTask<Void, Void, String>
  77.     {
  78.         @Override
  79.         protected void onPreExecute()
  80.         {
  81.             pDialog = new ProgressDialog(MainActivity.this);
  82.             pDialog.setIndeterminate(false);
  83.             pDialog.setCancelable(false);
  84.             pDialog.setMessage("Uploading..");
  85.             pDialog.show();
  86.         }
  87.        
  88.         @Override
  89.         protected String doInBackground(Void... unsued)
  90.         {
  91.             Bitmap imageUpload = BitmapFactory.decodeFile(picturePath);
  92.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  93.             imageUpload.compress(Bitmap.CompressFormat.JPEG, 60, baos);
  94.             byte[] image_data = baos.toByteArray();
  95.             String converted_image = Base64.encodeBytes(image_data);
  96.            
  97.             try
  98.             {
  99.                 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  100.                 nameValuePairs.add(new BasicNameValuePair("title", title));
  101.                 nameValuePairs.add(new BasicNameValuePair("images", converted_image));
  102.                 json = jsonParser.makeHttpRequest(url, "POST", nameValuePairs);
  103.             }
  104.             catch(Exception e)
  105.             {
  106.                 Log.i("INFO", e.toString());
  107.             }
  108.             return converted_image;
  109.         }
  110.        
  111.        
  112.         protected void onPostExecute(String encode)
  113.         {
  114.             byte_array.setText(encode);
  115.             pDialog.dismiss();
  116.         }
  117.     }
  118.     @Override
  119.     protected void onActivityResult(int requestCode, int resultCode, Intent data)
  120.     {
  121.         super.onActivityResult(requestCode, resultCode, data);
  122.         if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
  123.         {
  124.             selected_image = data.getData();
  125.             String[] filePath = {MediaStore.Images.Media.DATA};
  126.             cursor = getContentResolver().query(selected_image, filePath, null, null, null);
  127.             cursor.moveToFirst();
  128.            
  129.             int columnIndex = cursor.getColumnIndex(filePath[0]);
  130.             picturePath = cursor.getString(columnIndex);
  131.             cursor.close();
  132.             image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  133.         }
  134.     }
  135.    
  136.  
  137.     @Override
  138.     public boolean onCreateOptionsMenu(Menu menu) {
  139.         // Inflate the menu; this adds items to the action bar if it is present.
  140.         getMenuInflater().inflate(R.menu.main, menu);
  141.         return true;
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment