Android s @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(r

Android s @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(r,android,httprequest,Android,Httprequest,s @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Uri selectedImageUri = data.getData();

s
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            try {
                InputStream in = getContentResolver().openInputStream(selectedImageUri);
                OutputStream out = new FileOutputStream(new File("your_file_here"));

                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                TextView textViewDynamicText = (TextView) findViewById(R.id.textViewDynamicText); // Dynamic text
                String apiURL = "https://website.com/image_upload/image_upload.php";
                UploadImage task = new UploadImage(this, apiURL, out,
                        textViewDynamicText, new UploadImage.TaskListener() {
                    @Override
                    public void onFinished(String result) {
                        // Do Something after the task has finished
                        imageUploadResult();
                    }
                });
                task.execute();


                out.close();
                in.close();
            }
            catch (java.io.FileNotFoundException e) {
                Toast.makeText(this, "java.io.FileNotFoundException: " + e.toString(), Toast.LENGTH_LONG).show();
            }
            catch (java.io.IOException e) {
                Toast.makeText(this, "java.io.IOException: " + e.toString(), Toast.LENGTH_LONG).show();
            }

        } // RESULT_OK
    } // onActivityResult
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * Created by bruker on 08.08.2017.
 */

public class UploadImage extends AsyncTask<String, Void, String> {

    /* Class variables */
    private Context context; // Holder (this)
    private String  apiUrl; // URL for image upload form, example http://website.com/image_upload.php
    private TextView dynamicText;
    private OutputStream out;

    private final UploadImage.TaskListener taskListener; // This is the reference to the associated listener


    public interface TaskListener {
        public void onFinished(String result);
    }

    /*- Constructor GET, SEND --------------------------------------------------------------- */
    public UploadImage(Context ctx, String applicationPIUrl, OutputStream output, TextView textViewDynamicText, UploadImage.TaskListener listener) {
        context             = ctx;
        apiUrl              = applicationPIUrl;
        out                 = output;
        dynamicText         = textViewDynamicText;
        this.taskListener   = listener; // The listener reference is passed in through the constructor
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dynamicText.setText("Loading...");
    }


    @Override
    protected String doInBackground(String... params) {
        // Run methods
        String stringResponse ="";
        try {
            try{
                // Send image
                HttpRequest request = HttpRequest.post(apiUrl); // Post form
                request.part("inp_image", out); // send form image
                stringResponse = request.body();
            }
            catch (Exception e){
                return e.toString();
            }
        }
        catch(Exception e){
            return e.toString();
        }
        return stringResponse;
    }

    @Override
    protected void onPostExecute(String result) {
        // Set text view with result string
        if(dynamicText == null){
            Toast.makeText(context, "NULL", Toast.LENGTH_SHORT).show();
        }
        else {
            dynamicText.setText(result);
        }
        // In onPostExecute we check if the listener is valid
        if(this.taskListener != null) {
            // And if it is we call the callback function on it.
            this.taskListener.onFinished(result);
        }
    }

    @Override
    protected void onProgressUpdate(Void... values) {}

}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            try {
                InputStream in = getContentResolver().openInputStream(selectedImageUri);

                TextView textViewDynamicText = (TextView) findViewById(R.id.textViewDynamicText); // Dynamic text
                String apiURL = "https://website.com/image_upload/image_upload.php";
                UploadImage task = new UploadImage(this, apiURL, in,
                        textViewDynamicText, new UploadImage.TaskListener() {
                    @Override
                    public void onFinished(String result) {
                        // Do Something after the task has finished
                        imageUploadResult();
                    }
                });
                task.execute();

                in.close();
            }
            catch (java.io.FileNotFoundException e) {
                Toast.makeText(this, "java.io.FileNotFoundException: " + e.toString(), Toast.LENGTH_LONG).show();
            }
            catch (java.io.IOException e) {
                Toast.makeText(this, "java.io.IOException: " + e.toString(), Toast.LENGTH_LONG).show();
            }

        } // RESULT_OK
    } // onActivityResult
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.InputStream;

/**
 * Created by bruker on 08.08.2017.
 */

public class UploadImage extends AsyncTask<String, Void, String> {

    /* Class variables */
    private Context context; // Holder (this)
    private String  apiUrl; // URL for image upload form, example http://website.com/image_upload.php
    private TextView dynamicText;
    private InputStream in;

    private final UploadImage.TaskListener taskListener; // This is the reference to the associated listener


    public interface TaskListener {
        public void onFinished(String result);
    }

    /*- Constructor GET, SEND --------------------------------------------------------------- */
    public UploadImage(Context ctx, String applicationPIUrl, InputStream input, TextView textViewDynamicText, UploadImage.TaskListener listener) {
        context             = ctx;
        apiUrl              = applicationPIUrl;
        in                  = input;
        dynamicText         = textViewDynamicText;
        this.taskListener   = listener; // The listener reference is passed in through the constructor
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dynamicText.setText("Loading...");
    }


    @Override
    protected String doInBackground(String... params) {
        // Run methods
        String stringResponse ="";
        try {
            try{
                // Send image
                HttpRequest request = HttpRequest.post(apiUrl); // Post form
                request.part("inp_image", in); // send form image
                stringResponse = request.body();
            }
            catch (Exception e){
                return e.toString();
            }
        }
        catch(Exception e){
            return e.toString();
        }
        return stringResponse;
    }

    @Override
    protected void onPostExecute(String result) {
        // Set text view with result string
        if(dynamicText == null){
            Toast.makeText(context, "NULL", Toast.LENGTH_SHORT).show();
        }
        else {
            dynamicText.setText(result);
        }
        // In onPostExecute we check if the listener is valid
        if(this.taskListener != null) {
            // And if it is we call the callback function on it.
            this.taskListener.onFinished(result);
        }
    }

    @Override
    protected void onProgressUpdate(Void... values) {}

}