Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tobias;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.Toast;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- public class MainActivity extends AppCompatActivity {
- ImageView imageView;
- File imageFile;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.imagebig);
- File directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
- directoryPath.mkdirs();
- imageFile = new File(directoryPath, "testphoto.jpg");
- }
- public void HentFoto (View hest){
- Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- intent.setType("image/*");
- startActivityForResult(Intent.createChooser(intent, "Select Photo"), 3);
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == 3 && resultCode == RESULT_OK && data != null && data.getData() != null) {
- Uri uri = data.getData();
- try {
- Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
- imageView.setImageBitmap(bitmap);
- //Gem billede
- FileOutputStream fos;
- fos = new FileOutputStream(imageFile);
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
- fos.flush();
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void Upload(View view) {
- new UploadImageTask().execute("foto"); //Filnavn
- }
- private class UploadImageTask extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... strings) {
- String data = "";
- HttpURLConnection connection = null;
- DataOutputStream outputStream = null;
- String lineEnd = "\r\n";
- String twoHyphens = "--";
- String boundary = "*****";
- int bytesRead, bytesAvailable, bufferSize;
- byte[] buffer;
- int maxBufferSize = 1 * 1024 * 1024;
- try { // open stream to local file
- FileInputStream fileInputStream;
- fileInputStream = new FileInputStream(imageFile);
- String fileName = strings[0];
- String urlServer = "http://bdeapps.dk/android/mikkelgeo/imageupload.php";
- URL url = new URL(urlServer);
- connection = (HttpURLConnection) url.openConnection();
- connection.setDoInput(true);
- connection.setDoOutput(true);
- connection.setUseCaches(false);
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Connection", "Keep-Alive");
- connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
- outputStream = new DataOutputStream(connection.getOutputStream());
- outputStream.writeBytes(twoHyphens + boundary + lineEnd);
- outputStream.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\"" + fileName + ".jpg" + "\"" + lineEnd + lineEnd);
- bytesAvailable = fileInputStream.available();
- bufferSize = Math.min(bytesAvailable, maxBufferSize);
- buffer = new byte[bufferSize];
- bytesRead = fileInputStream.read(buffer, 0, bufferSize);
- while (bytesRead > 0) {
- outputStream.write(buffer, 0, bufferSize);
- bytesAvailable = fileInputStream.available();
- bufferSize = Math.min(bytesAvailable, maxBufferSize);
- bytesRead = fileInputStream.read(buffer, 0, bufferSize);
- }
- outputStream.writeBytes(lineEnd);
- outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
- int serverResponseCode = connection.getResponseCode(); // Responses from the server (code and message)
- String serverResponseMessage = connection.getResponseMessage(); //Transfer success: code = 200. Message = OK
- if (serverResponseCode == 200 && serverResponseMessage.equals("OK")) {
- }
- fileInputStream.close();
- outputStream.flush();
- outputStream.close();
- connection.disconnect();
- } catch (Exception e) {data = e.toString();}
- return data;
- }
- @Override
- protected void onPostExecute(String result) {
- Toast.makeText(getApplicationContext(), "Nemt", Toast.LENGTH_LONG).show();
- }
- }
- public void Download(View view) {
- new DownloadImageTask().execute("foto");
- }
- private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
- @Override
- protected Bitmap doInBackground(String... strings) {
- Bitmap bmImg = null;
- URL myFileUrl = null;
- try {
- String fileName = strings[0];
- String imageUrl = "http://bdeapps.dk/android/mikkelgeo/Images/" + fileName + ".jpg";
- myFileUrl = new URL(imageUrl);
- } catch (MalformedURLException e) {}
- try {
- HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
- conn.setDoInput(true);
- conn.connect();
- InputStream is = conn.getInputStream();
- bmImg = BitmapFactory.decodeStream(is);
- is.close();
- conn.disconnect();
- } catch (IOException e) {}
- return bmImg;
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- imageView.setImageBitmap(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement