Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.uploadimage;
- import java.io.File;
- import java.io.ByteArrayOutputStream;
- import java.util.ArrayList;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONObject;
- import android.os.Bundle;
- import android.os.AsyncTask;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.view.Menu;
- import android.view.View;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.provider.MediaStore;
- import android.util.Log;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.database.Cursor;
- public class MainActivity extends Activity {
- ImageView image;
- Button upload, browse;
- EditText caption, byte_array;
- Intent intent;
- Uri selected_image;
- File file;
- Cursor cursor;
- ProgressDialog pDialog;
- JSONObject json;
- JSONParser jsonParser = new JSONParser();
- String picturePath, title;
- private static int RESULT_LOAD_IMAGE = 1;
- private static final String url = "http://192.168.1.8/psn_asset_oracle/images.php";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- image = (ImageView)findViewById(R.id.image);
- upload = (Button)findViewById(R.id.upload);
- browse = (Button)findViewById(R.id.browse);
- caption = (EditText)findViewById(R.id.caption);
- byte_array = (EditText)findViewById(R.id.byte_array);
- browse.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, RESULT_LOAD_IMAGE);
- }
- });
- upload.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- title = caption.getText().toString();
- new upload_image().execute();
- }
- });
- }
- class upload_image extends AsyncTask<Void, Void, String>
- {
- @Override
- protected void onPreExecute()
- {
- pDialog = new ProgressDialog(MainActivity.this);
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(false);
- pDialog.setMessage("Uploading..");
- pDialog.show();
- }
- @Override
- protected String doInBackground(Void... unsued)
- {
- Bitmap imageUpload = BitmapFactory.decodeFile(picturePath);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- imageUpload.compress(Bitmap.CompressFormat.JPEG, 60, baos);
- byte[] image_data = baos.toByteArray();
- String converted_image = Base64.encodeBytes(image_data);
- try
- {
- ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
- nameValuePairs.add(new BasicNameValuePair("title", title));
- nameValuePairs.add(new BasicNameValuePair("images", converted_image));
- json = jsonParser.makeHttpRequest(url, "POST", nameValuePairs);
- }
- catch(Exception e)
- {
- Log.i("INFO", e.toString());
- }
- return converted_image;
- }
- protected void onPostExecute(String encode)
- {
- byte_array.setText(encode);
- pDialog.dismiss();
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
- {
- selected_image = data.getData();
- String[] filePath = {MediaStore.Images.Media.DATA};
- cursor = getContentResolver().query(selected_image, filePath, null, null, null);
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndex(filePath[0]);
- picturePath = cursor.getString(columnIndex);
- cursor.close();
- image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment