Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.loginphase;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.mime.MultipartEntity;
- import org.apache.http.entity.mime.content.ByteArrayBody;
- import org.apache.http.entity.mime.content.StringBody;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.app.Activity;
- import android.app.Fragment;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- import android.widget.Toast;
- import database.DriveDatabaseAdapter;
- public class SpaceFragment extends Fragment {
- public SpaceFragment(){
- }
- private MenuItem item;
- private String url = "http://172.16.161.64:8084/ServerUpDown/TestServlet";
- ListView listContent;
- SimpleCursorAdapter cursorAdapter;
- Cursor cursor;
- private DriveDatabaseAdapter mySQLiteAdapter;
- final private int PICK_IMAGE = 1;
- private String selectedImage;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- final View rootView = inflater.inflate(R.layout.fragment_space, container, false);
- getActivity().getActionBar().setTitle("Space");
- listContent = (ListView)rootView.findViewById(R.id.uploadedlist);
- Button btnUpload = (Button) rootView.findViewById(R.id.btnUpload);
- mySQLiteAdapter = new DriveDatabaseAdapter(rootView.getContext());
- mySQLiteAdapter.openToWrite();
- SharedPreferences pref = rootView.getContext().getSharedPreferences("currentuser", 0);
- String name = pref.getString("username", null);
- cursor = mySQLiteAdapter.queueAll(name);
- String[] from = new String[]{DriveDatabaseAdapter.FILE_NAME, DriveDatabaseAdapter.FILE_SIZE,DriveDatabaseAdapter.FILE_UPLOAD_TIME};
- int[] to = new int[]{R.id.filenamelbl, R.id.filesizelbl,R.id.filetimelbl};
- cursorAdapter =
- new SimpleCursorAdapter(rootView.getContext(), R.layout.uploadedfiles_list, cursor, from, to);
- listContent.setAdapter(cursorAdapter);
- //updateList();
- //cursorAdapter.notifyDataSetChanged();
- btnUpload.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(i,PICK_IMAGE);
- }
- });
- listContent.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- Toast.makeText(rootView.getContext(), "CLICKED "+position, Toast.LENGTH_LONG).show();
- Intent i = new Intent(rootView.getContext(),DownloadActivity.class);
- i.putExtra("position", position);
- startActivity(i);
- }});
- return rootView;
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == Activity.RESULT_OK) {
- if (requestCode == PICK_IMAGE) {
- Toast.makeText(getActivity().getApplicationContext(), "IMAGED PICKED", Toast.LENGTH_LONG).show();
- selectedImage = getAbsolutePath(data.getData());
- File f = new File(selectedImage);
- long fileSize=f.length();
- String sizefile = size(fileSize);
- //Toast.makeText(getActivity().getApplicationContext(), "IMAGE NAME "+selectedImage, Toast.LENGTH_LONG).show();
- Bitmap bmp = BitmapFactory.decodeFile(selectedImage);
- String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
- Uploader uploader = new Uploader(selectedImage, sizefile, date);
- uploader.upload();
- /*Intent i = new Intent(getActivity().getBaseContext(),UploadActivity.class);
- i.putExtra("filename", selectedImage);
- i.putExtra("fileSize", sizefile);
- i.putExtra("fileTime", date);
- Toast.makeText(getActivity().getApplicationContext(), "IMAGE "+selectedImage+","+fileSize+","+date, Toast.LENGTH_LONG).show();
- startActivity(i);
- /*
- Fragment newFragment = new uploadFragment();
- Bundle cc = new Bundle();
- cc.putString("filename", selectedImage);
- newFragment.setArguments(cc);
- android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
- transaction.replace(R.id.frame_container, newFragment);
- transaction.addToBackStack(null);
- transaction.commit();
- */
- } else {
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
- }
- public String getAbsolutePath(Uri uri) {
- String[] projection = {MediaStore.Images.Media.DATA};
- Cursor cursor = getActivity().getContentResolver().query(uri,
- projection, null, null, null);
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndex(projection[0]);
- return cursor.getString(columnIndex);
- }
- public String size(long size){
- String hrSize = "";
- double m = size/1024.0;
- DecimalFormat dec = new DecimalFormat("0.00");
- if (m > 1) {
- hrSize = dec.format(m).concat(" KB");
- } else {
- hrSize = dec.format(size).concat("B");
- }
- return hrSize;
- }
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- // TODO Auto-generated method stub
- //inflater.inflate(R.menu.main, menu);
- //item = menu.getItem(0);
- }
- class Uploader{
- String selectedImagePath;
- String fileSize;
- String date;
- Uploader(String selectedImagePath, String fileSize, String date){
- this.selectedImagePath=selectedImagePath;
- this.fileSize=fileSize;
- this.date=date;
- }
- void upload(){
- SharedPreferences pref = SpaceFragment.this.getActivity().getSharedPreferences("currentuser", 0);
- String name = pref.getString("username", null);
- String param2 = name;
- String fileNameArray[] = selectedImagePath.split("/");
- String fileName = fileNameArray[fileNameArray.length-1];
- SendHttpRequestTask t = new SendHttpRequestTask();
- Toast.makeText(SpaceFragment.this.getActivity(), "CURRENTLY UPLOADING "+selectedImage+","+param2, Toast.LENGTH_LONG).show();
- String[] params = new String[]{url, selectedImagePath, param2};
- t.execute(params);
- }
- private class SendHttpRequestTask extends AsyncTask<String, Void, String> {
- ProgressDialog progressDialog;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- progressDialog = new ProgressDialog(SpaceFragment.this.getActivity());
- progressDialog.setMessage("Uploading File. Please wait");
- progressDialog.setCancelable(false);
- progressDialog.show();
- }
- @Override
- protected String doInBackground(String... params) {
- String url = params[0];
- String param1 = params[1];
- String param2 = params[2];
- String fileNameArray[] = param1.split("/");
- String fileName = fileNameArray[fileNameArray.length-1];
- try {
- Bitmap b = BitmapFactory.decodeFile(param1);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- b.compress(CompressFormat.PNG, 0, baos);
- HttpClient client = new DefaultHttpClient();
- HttpPost post = new HttpPost(url);
- MultipartEntity multiPart = new MultipartEntity();
- multiPart.addPart("param1", new StringBody(param1));
- multiPart.addPart("param2", new StringBody(param2));
- //Toast.makeText(getApplicationContext(),"SIZE IS "+ baos.toByteArray().length, Toast.LENGTH_LONG).show();
- multiPart.addPart("file", new ByteArrayBody(baos.toByteArray(), fileName));
- post.setEntity(multiPart);
- client.execute(post);
- }
- catch(Throwable t) {
- t.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(String data) {
- progressDialog.dismiss();
- //SpaceFragment.this.cursor.requery();
- //SpaceFragment.this.cursorAdapter.notifyDataSetChanged();
- mySQLiteAdapter = new DriveDatabaseAdapter(SpaceFragment.this.getActivity().getApplicationContext());
- mySQLiteAdapter.openToWrite();
- SharedPreferences pref = SpaceFragment.this.getActivity().getSharedPreferences("currentuser", 0);
- String name = pref.getString("username", null);
- String fileNameArray[] = selectedImagePath.split("/");
- String fileName = fileNameArray[fileNameArray.length-1];
- mySQLiteAdapter.insertEntry(name,"IMAGE",fileName,fileSize,date);
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement