2

After some research I came across an open library for multipart file uploading. In my case I want to upload an Image using PUT request which the images are either choose from gallery or by camera. These are the resources I am using: 1. https://github.com/gotev/android-upload-service 2. https://www.simplifiedcoding.net/android-upload-image-to-server/#comment-9852

ProfileSetting.java

public class ProfileSetting extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private ImageView CustomerIcon;
private Button confirm_button;
//storage permission code
private static final int STORAGE_PERMISSION_CODE = 123;
//Bitmap to get image from gallery
private Bitmap bitmap;
//Uri to store the image uri
private Uri selectedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_setting);

    //Requesting storage permission
    requestStoragePermission();

confirm_button.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View view) {


            uploadMultipart();
            //PUT VOLLEY
            //saveProfileAccount();

        }
    });
}
private void showPickImageDialog() {
    AlertDialog.Builder builderSingle = new AlertDialog.Builder(ProfileSetting.this);
    builderSingle.setTitle("Set Image   ");

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            ProfileSetting.this,
            android.R.layout.select_dialog_singlechoice);
    arrayAdapter.add("Gallery");
    arrayAdapter.add("Camera");

    builderSingle.setNegativeButton(
            "cancel",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    builderSingle.setAdapter(
            arrayAdapter,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(pickPhoto, 1);
                            break;

                        case 1:
                            Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(takePicture, 0);
                            break;
                    }

                }
            });
    builderSingle.show();
}
 protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch(requestCode) {
        case 0:
            if(resultCode == RESULT_OK){


                selectedImage = imageReturnedIntent.getData();
                //CustomerIcon.setImageURI(selectedImage);
                try{
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
                    CustomerIcon.setImageBitmap(bitmap);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            break;
        case 1:
            if(resultCode == RESULT_OK){

                Uri selectedImage = imageReturnedIntent.getData();
                //CustomerIcon.setImageURI(selectedImage);
                try{
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
                    //bitmap = bitmap.createScaledBitmap(bitmap, 150, 150, true);
                    CustomerIcon.setImageBitmap(bitmap);




                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            break;
    }
}
    /*
* This is the method responsible for image upload
* We need the full image path and the name for the image in this     method
* */
public void uploadMultipart() {
    //getting name for the image
    String name = "customer_icon";

    //getting the actual path of the image
    String path = getPath(selectedImage);

    //Uploading code
    try {
        String uploadId = UUID.randomUUID().toString();

        //Creating a multi part request
        new MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
                .addFileToUpload(path, "image") //Adding file
                .addParameter("name", name) //Adding text parameter to the request
                .setNotificationConfig(new UploadNotificationConfig())
                .setMaxRetries(2)
                .startUpload(); //Starting the upload

    } catch (Exception exc) {
        Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

//method to get the file path from uri
public String getPath(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
    cursor.close();

    cursor = getContentResolver().query(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();

    return path;
}

//Requesting permission
private void requestStoragePermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
        return;

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }
    //And finally ask for the permission
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}

//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    //Checking the request code of our request
    if (requestCode == STORAGE_PERMISSION_CODE) {

        //If permission is granted
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Displaying a toast
            Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
        } else {
            //Displaying another toast if permission is not granted
            Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
        }
    }
}
}

I am getting this error :

FATAL EXCEPTION: main
java.lang.NullPointerException: uri

Also I am not sure how to set headers for authorization in this method. Please help, thank you!

JerryKo
  • 384
  • 7
  • 25

4 Answers4

2
String url = " http://server.yourserver.com/v1/example";

    try {
        String uploadId = UUID.randomUUID().toString();

        //Creating a multi part request
        MultipartUploadRequest request = new MultipartUploadRequest(this, uploadId, UPLOAD_URL);

        request.addFileToUpload(images.getPath(), "image_url");
        request.setNotificationConfig(new UploadNotificationConfig());
        request.setMaxRetries(2);
        request.startUpload(); //Starting the upload

    } catch (Exception exc) {
        Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
    }
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
1

check following code.(I used okHttp library).

  private int uploadFile(String imagePath) {
    Log.i("PATH",imagePath);
    OkHttpClient client = new OkHttpClient();
    File fileSource = new File(imagePath);
    if (fileSource.isFile()){
        Log.i("EXIST","exist");
    }else {
        Log.i("NOT EXIST","not exist");
    }
    final MediaType MEDIA_TYPE;
    String imageType;
    if (imagePath.endsWith("png")){
        MEDIA_TYPE = MediaType.parse("image/png");
        imageType = ".png";
    }else {
        MEDIA_TYPE = MediaType.parse("image/jpeg");
        imageType = ".jpg";
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
    String fileName = "Prathap_"+timeStamp+imageType;


    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)   .addFormDataPart("file",fileName,RequestBody.create(MEDIA_TYPE,fileSource))
            .build();
    Request request = new Request.Builder()
            .url(your url)//your webservice url
            .post(requestBody)
            .build();
    try {
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()){
            Log.i("SUCC",""+response.message());
        }
        String resp = response.message();
        Log.i("MSG",resp);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

add the below line in build.gradle under app folder.

compile 'com.squareup.okhttp3:okhttp:3.5.0'
Prathap Badavath
  • 1,621
  • 2
  • 20
  • 24
1

First in build.gradle add this:

implementation "net.gotev:uploadservice:4.7.0"

then in onCreate() add code as bellow:

UploadServiceConfig.initialize(this.getApplication(), "UploadJSON",true);

then make two functions as bellow:

public void uploadJSONFile(String filePath, String UPLOAD_URL){

        try {
            String uploadId = UUID.randomUUID().toString();
            //Creating a multi part request
            MultipartUploadRequest request = new MultipartUploadRequest(MainActivity.this, UPLOAD_URL);
            request.setMethod("POST");
            request.addParameter("par1","1");
            request.addHeader("hed","val");
            request.addFileToUpload(filePath, "image_url");
            request.setNotificationConfig(createNotificationChannel());
            //request.setMaxRetries(2);
            request.startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private Function2<? super Context, ? super String, UploadNotificationConfig> createNotificationChannel() {

        Function2<? super Context, ? super String, UploadNotificationConfig> test = null;
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Toast.makeText(this, "Build.VERSION.SDK_INT : YES", Toast.LENGTH_SHORT).show();
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("UploadJSON", name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);

            UploadNotificationStatusConfig uploadNotificationStatusConfig = new UploadNotificationStatusConfig("Upload JSON",
                    "uploading...");
            UploadNotificationStatusConfig uploadNotificationStatusConfigSucces = new UploadNotificationStatusConfig("Upload JSON",
                    "finished!");
            UploadNotificationStatusConfig uploadNotificationStatusConfigError = new UploadNotificationStatusConfig("Upload JSON",
                    "Error!");
            UploadNotificationStatusConfig uploadNotificationStatusConfigCancele = new UploadNotificationStatusConfig("Upload JSON",
                    "Canceled");

            uploadNotificationConfig = new UploadNotificationConfig("UploadJSON",
                    true,
                    uploadNotificationStatusConfig,uploadNotificationStatusConfigSucces,
                    uploadNotificationStatusConfigError,
                    uploadNotificationStatusConfigCancele
            );

            test = new Function2<Context, String, UploadNotificationConfig>() {
                @Override
                public UploadNotificationConfig invoke(Context context, String s) {
                    return uploadNotificationConfig;
                }
            };

        }else{

        }
        return test;
    }

now call uploadJSONFile().

Ali Hosein pour
  • 220
  • 4
  • 19
0

you can use retrofit's Multipart.Body for image uploading to server. This link will guide you how to upload files using multipart.

swetabh suman
  • 1,949
  • 2
  • 19
  • 24