Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. package com.tobias;
  2.  
  3. import android.content.Intent;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.net.Uri;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.os.Environment;
  10. import android.provider.MediaStore;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.view.View;
  13. import android.widget.ImageView;
  14. import android.widget.Toast;
  15.  
  16. import java.io.DataOutputStream;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.net.HttpURLConnection;
  23. import java.net.MalformedURLException;
  24. import java.net.URL;
  25.  
  26. public class MainActivity extends AppCompatActivity {
  27.     ImageView imageView;
  28.     File imageFile;
  29.  
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_main);
  34.  
  35.         imageView = (ImageView) findViewById(R.id.imagebig);
  36.  
  37.         File directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  38.         directoryPath.mkdirs();
  39.         imageFile = new File(directoryPath, "testphoto.jpg");
  40.  
  41.     }
  42.  
  43.     public void HentFoto (View hest){
  44.         Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  45.         intent.setType("image/*");
  46.         startActivityForResult(Intent.createChooser(intent, "Select Photo"), 3);
  47.  
  48.     }
  49.  
  50.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  51.         super.onActivityResult(requestCode, resultCode, data);
  52.  
  53.         if (requestCode == 3 && resultCode == RESULT_OK && data != null && data.getData() != null) {
  54.             Uri uri = data.getData();
  55.             try {
  56.                 Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
  57.                 imageView.setImageBitmap(bitmap);
  58.                 //Gem billede
  59.                 FileOutputStream fos;
  60.                 fos = new FileOutputStream(imageFile);
  61.                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  62.                 fos.flush();
  63.                 fos.close();
  64.             } catch (IOException e) {
  65.                 e.printStackTrace();
  66.             }
  67.         }
  68.     }
  69.  
  70.     public void Upload(View view) {
  71.         new UploadImageTask().execute("foto"); //Filnavn
  72.     }
  73.  
  74.     private class UploadImageTask extends AsyncTask<String, Void, String> {
  75.         @Override
  76.         protected String doInBackground(String... strings) {
  77.             String data = "";
  78.             HttpURLConnection connection = null;
  79.             DataOutputStream outputStream = null;
  80.             String lineEnd = "\r\n";
  81.             String twoHyphens = "--";
  82.             String boundary = "*****";
  83.             int bytesRead, bytesAvailable, bufferSize;
  84.             byte[] buffer;
  85.             int maxBufferSize = 1 * 1024 * 1024;
  86.             try { // open stream to local file
  87.                 FileInputStream fileInputStream;
  88.                 fileInputStream = new FileInputStream(imageFile);
  89.                 String fileName = strings[0];
  90.  
  91.                 String urlServer = "http://bdeapps.dk/android/mikkelgeo/imageupload.php";
  92.  
  93.                 URL url = new URL(urlServer);
  94.                 connection = (HttpURLConnection) url.openConnection();
  95.                 connection.setDoInput(true);
  96.                 connection.setDoOutput(true);
  97.                 connection.setUseCaches(false);
  98.                 connection.setRequestMethod("POST");
  99.                 connection.setRequestProperty("Connection", "Keep-Alive");
  100.                 connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
  101.                 outputStream = new DataOutputStream(connection.getOutputStream());
  102.                 outputStream.writeBytes(twoHyphens + boundary + lineEnd);
  103.                 outputStream.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\"" + fileName + ".jpg" + "\"" + lineEnd + lineEnd);
  104.                 bytesAvailable = fileInputStream.available();
  105.                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
  106.                 buffer = new byte[bufferSize];
  107.                 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  108.                 while (bytesRead > 0) {
  109.                     outputStream.write(buffer, 0, bufferSize);
  110.                     bytesAvailable = fileInputStream.available();
  111.                     bufferSize = Math.min(bytesAvailable, maxBufferSize);
  112.                     bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  113.                 }
  114.                 outputStream.writeBytes(lineEnd);
  115.                 outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  116.                 int serverResponseCode = connection.getResponseCode(); // Responses from the server (code and message)
  117.                 String serverResponseMessage = connection.getResponseMessage(); //Transfer success: code = 200. Message = OK
  118.                 if (serverResponseCode == 200 && serverResponseMessage.equals("OK")) {
  119.                 }
  120.                 fileInputStream.close();
  121.                 outputStream.flush();
  122.                 outputStream.close();
  123.                 connection.disconnect();
  124.             } catch (Exception e) {data = e.toString();}
  125.             return data;
  126.         }
  127.         @Override
  128.         protected void onPostExecute(String result) {
  129.             Toast.makeText(getApplicationContext(), "Nemt", Toast.LENGTH_LONG).show();
  130.         }
  131.     }
  132.  
  133.     public void Download(View view) {
  134.         new DownloadImageTask().execute("foto");
  135.     }
  136.  
  137.     private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
  138.         @Override
  139.         protected Bitmap doInBackground(String... strings) {
  140.             Bitmap bmImg = null;
  141.             URL myFileUrl = null;
  142.             try {
  143.                 String fileName = strings[0];
  144.                 String imageUrl = "http://bdeapps.dk/android/mikkelgeo/Images/" + fileName + ".jpg";
  145.                 myFileUrl = new URL(imageUrl);
  146.             } catch (MalformedURLException e) {}
  147.             try {
  148.                 HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
  149.                 conn.setDoInput(true);
  150.                 conn.connect();
  151.                 InputStream is = conn.getInputStream();
  152.                 bmImg = BitmapFactory.decodeStream(is);
  153.                 is.close();
  154.                 conn.disconnect();
  155.             } catch (IOException e) {}
  156.             return bmImg;
  157.         }
  158.         @Override
  159.         protected void onPostExecute(Bitmap result) {
  160.             imageView.setImageBitmap(result);
  161.         }
  162.     }
  163.  
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement