Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. package com.example.loginphase;
  2.  
  3.  
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.text.DecimalFormat;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9.  
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.entity.mime.MultipartEntity;
  13. import org.apache.http.entity.mime.content.ByteArrayBody;
  14. import org.apache.http.entity.mime.content.StringBody;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16.  
  17. import android.app.Activity;
  18. import android.app.Fragment;
  19. import android.app.ProgressDialog;
  20. import android.content.Intent;
  21. import android.content.SharedPreferences;
  22. import android.database.Cursor;
  23. import android.graphics.Bitmap;
  24. import android.graphics.Bitmap.CompressFormat;
  25. import android.graphics.BitmapFactory;
  26. import android.net.Uri;
  27. import android.os.AsyncTask;
  28. import android.os.Bundle;
  29. import android.provider.MediaStore;
  30. import android.view.LayoutInflater;
  31. import android.view.Menu;
  32. import android.view.MenuInflater;
  33. import android.view.MenuItem;
  34. import android.view.View;
  35. import android.view.ViewGroup;
  36. import android.widget.AdapterView;
  37. import android.widget.AdapterView.OnItemClickListener;
  38. import android.widget.Button;
  39. import android.widget.ListView;
  40. import android.widget.SimpleCursorAdapter;
  41. import android.widget.Toast;
  42. import database.DriveDatabaseAdapter;
  43.  
  44.  
  45. public class SpaceFragment extends Fragment {
  46.  
  47. public SpaceFragment(){
  48.  
  49.  
  50. }
  51. private MenuItem item;
  52. private String url = "http://172.16.161.64:8084/ServerUpDown/TestServlet";
  53. ListView listContent;
  54. SimpleCursorAdapter cursorAdapter;
  55. Cursor cursor;
  56. private DriveDatabaseAdapter mySQLiteAdapter;
  57. final private int PICK_IMAGE = 1;
  58. private String selectedImage;
  59. @Override
  60. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  61. Bundle savedInstanceState) {
  62.  
  63. final View rootView = inflater.inflate(R.layout.fragment_space, container, false);
  64. getActivity().getActionBar().setTitle("Space");
  65. listContent = (ListView)rootView.findViewById(R.id.uploadedlist);
  66. Button btnUpload = (Button) rootView.findViewById(R.id.btnUpload);
  67. mySQLiteAdapter = new DriveDatabaseAdapter(rootView.getContext());
  68. mySQLiteAdapter.openToWrite();
  69. SharedPreferences pref = rootView.getContext().getSharedPreferences("currentuser", 0);
  70. String name = pref.getString("username", null);
  71.  
  72. cursor = mySQLiteAdapter.queueAll(name);
  73. String[] from = new String[]{DriveDatabaseAdapter.FILE_NAME, DriveDatabaseAdapter.FILE_SIZE,DriveDatabaseAdapter.FILE_UPLOAD_TIME};
  74. int[] to = new int[]{R.id.filenamelbl, R.id.filesizelbl,R.id.filetimelbl};
  75. cursorAdapter =
  76. new SimpleCursorAdapter(rootView.getContext(), R.layout.uploadedfiles_list, cursor, from, to);
  77. listContent.setAdapter(cursorAdapter);
  78.  
  79. //updateList();
  80. //cursorAdapter.notifyDataSetChanged();
  81.  
  82. btnUpload.setOnClickListener(new View.OnClickListener() {
  83. @Override
  84. public void onClick(View v) {
  85. Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  86. startActivityForResult(i,PICK_IMAGE);
  87.  
  88.  
  89. }
  90. });
  91.  
  92. listContent.setOnItemClickListener(new OnItemClickListener() {
  93. @Override
  94. public void onItemClick(AdapterView<?> parent, View view,
  95. int position, long id) {
  96. Toast.makeText(rootView.getContext(), "CLICKED "+position, Toast.LENGTH_LONG).show();
  97. Intent i = new Intent(rootView.getContext(),DownloadActivity.class);
  98. i.putExtra("position", position);
  99. startActivity(i);
  100.  
  101. }});
  102.  
  103. return rootView;
  104. }
  105.  
  106. @Override
  107. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  108. // TODO Auto-generated method stub
  109. super.onActivityResult(requestCode, resultCode, data);
  110. if (resultCode == Activity.RESULT_OK) {
  111. if (requestCode == PICK_IMAGE) {
  112.  
  113. Toast.makeText(getActivity().getApplicationContext(), "IMAGED PICKED", Toast.LENGTH_LONG).show();
  114.  
  115.  
  116. selectedImage = getAbsolutePath(data.getData());
  117. File f = new File(selectedImage);
  118. long fileSize=f.length();
  119. String sizefile = size(fileSize);
  120. //Toast.makeText(getActivity().getApplicationContext(), "IMAGE NAME "+selectedImage, Toast.LENGTH_LONG).show();
  121.  
  122. Bitmap bmp = BitmapFactory.decodeFile(selectedImage);
  123.  
  124. String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
  125.  
  126. Uploader uploader = new Uploader(selectedImage, sizefile, date);
  127. uploader.upload();
  128. /*Intent i = new Intent(getActivity().getBaseContext(),UploadActivity.class);
  129. i.putExtra("filename", selectedImage);
  130. i.putExtra("fileSize", sizefile);
  131. i.putExtra("fileTime", date);
  132. Toast.makeText(getActivity().getApplicationContext(), "IMAGE "+selectedImage+","+fileSize+","+date, Toast.LENGTH_LONG).show();
  133.  
  134. startActivity(i);
  135.  
  136. /*
  137. Fragment newFragment = new uploadFragment();
  138. Bundle cc = new Bundle();
  139. cc.putString("filename", selectedImage);
  140. newFragment.setArguments(cc);
  141. android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
  142. transaction.replace(R.id.frame_container, newFragment);
  143. transaction.addToBackStack(null);
  144. transaction.commit();
  145.  
  146. */
  147. } else {
  148. super.onActivityResult(requestCode, resultCode, data);
  149. }
  150. }
  151. }
  152. public String getAbsolutePath(Uri uri) {
  153. String[] projection = {MediaStore.Images.Media.DATA};
  154. Cursor cursor = getActivity().getContentResolver().query(uri,
  155. projection, null, null, null);
  156. cursor.moveToFirst();
  157.  
  158. int columnIndex = cursor.getColumnIndex(projection[0]);
  159. return cursor.getString(columnIndex);
  160.  
  161. }
  162.  
  163. public String size(long size){
  164. String hrSize = "";
  165. double m = size/1024.0;
  166. DecimalFormat dec = new DecimalFormat("0.00");
  167.  
  168. if (m > 1) {
  169. hrSize = dec.format(m).concat(" KB");
  170. } else {
  171. hrSize = dec.format(size).concat("B");
  172. }
  173. return hrSize;
  174. }
  175. @Override
  176. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  177. // TODO Auto-generated method stub
  178.  
  179. //inflater.inflate(R.menu.main, menu);
  180. //item = menu.getItem(0);
  181. }
  182.  
  183.  
  184. class Uploader{
  185. String selectedImagePath;
  186. String fileSize;
  187. String date;
  188.  
  189. Uploader(String selectedImagePath, String fileSize, String date){
  190. this.selectedImagePath=selectedImagePath;
  191. this.fileSize=fileSize;
  192. this.date=date;
  193. }
  194.  
  195. void upload(){
  196. SharedPreferences pref = SpaceFragment.this.getActivity().getSharedPreferences("currentuser", 0);
  197. String name = pref.getString("username", null);
  198. String param2 = name;
  199. String fileNameArray[] = selectedImagePath.split("/");
  200. String fileName = fileNameArray[fileNameArray.length-1];
  201. SendHttpRequestTask t = new SendHttpRequestTask();
  202. Toast.makeText(SpaceFragment.this.getActivity(), "CURRENTLY UPLOADING "+selectedImage+","+param2, Toast.LENGTH_LONG).show();
  203. String[] params = new String[]{url, selectedImagePath, param2};
  204. t.execute(params);
  205. }
  206.  
  207.  
  208. private class SendHttpRequestTask extends AsyncTask<String, Void, String> {
  209. ProgressDialog progressDialog;
  210. @Override
  211. protected void onPreExecute() {
  212. super.onPreExecute();
  213. progressDialog = new ProgressDialog(SpaceFragment.this.getActivity());
  214. progressDialog.setMessage("Uploading File. Please wait");
  215. progressDialog.setCancelable(false);
  216. progressDialog.show();
  217.  
  218. }
  219. @Override
  220. protected String doInBackground(String... params) {
  221. String url = params[0];
  222. String param1 = params[1];
  223. String param2 = params[2];
  224. String fileNameArray[] = param1.split("/");
  225. String fileName = fileNameArray[fileNameArray.length-1];
  226. try {
  227. Bitmap b = BitmapFactory.decodeFile(param1);
  228. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  229. b.compress(CompressFormat.PNG, 0, baos);
  230. HttpClient client = new DefaultHttpClient();
  231. HttpPost post = new HttpPost(url);
  232. MultipartEntity multiPart = new MultipartEntity();
  233. multiPart.addPart("param1", new StringBody(param1));
  234. multiPart.addPart("param2", new StringBody(param2));
  235. //Toast.makeText(getApplicationContext(),"SIZE IS "+ baos.toByteArray().length, Toast.LENGTH_LONG).show();
  236. multiPart.addPart("file", new ByteArrayBody(baos.toByteArray(), fileName));
  237. post.setEntity(multiPart);
  238. client.execute(post);
  239. }
  240. catch(Throwable t) {
  241. t.printStackTrace();
  242. }
  243. return null;
  244. }
  245. @Override
  246. protected void onPostExecute(String data) {
  247. progressDialog.dismiss();
  248. //SpaceFragment.this.cursor.requery();
  249. //SpaceFragment.this.cursorAdapter.notifyDataSetChanged();
  250. mySQLiteAdapter = new DriveDatabaseAdapter(SpaceFragment.this.getActivity().getApplicationContext());
  251. mySQLiteAdapter.openToWrite();
  252. SharedPreferences pref = SpaceFragment.this.getActivity().getSharedPreferences("currentuser", 0);
  253. String name = pref.getString("username", null);
  254. String fileNameArray[] = selectedImagePath.split("/");
  255. String fileName = fileNameArray[fileNameArray.length-1];
  256. mySQLiteAdapter.insertEntry(name,"IMAGE",fileName,fileSize,date);
  257. }
  258. }
  259. };
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement