0

Hi i'm struggling to find a way to upload a file directly form android phone camera to my web server (accept multipart).

Due to regulations in my country for fintech apps, i'm not allowed to ask the user for write and read storage permissions so i have to upload it directly after taking the pictures without saving it in storage.

This is what i currently have.

profileCameraImageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(areAllPermissionsGranted()) {
                            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                                profileFile = new File(getActivity().getApplicationContext().getExternalFilesDir(
                                        android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + File.separator + "profile.jpg");
                                profileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
                                        authorities,
                                        profileFile);
                                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, profileUri);
                                startActivityForResult(takePictureIntent, PICK_CAMERA_PROFILE);
                    }    

As well as the multipart creation method, i just passed the profileFile obtained from the code above to here

public MultipartBody.Part prepareFilePartFromCamera(String partName, File file) {

        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
        return body;
    }

Everytime i tried to upload to my webserver, it always tell me that it can't accept the file format. Sometimes i saw that i need write permission in my logcat.

Many thanks for your help

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

Reading and Writing from external storage needs Permission which is dangerous permission that needs acceptance from the user at runtime.

The solution for that is to save the image to the internal storage instead of external storage. This does not need permission from the user and you can use it as a file and upload it to the server. this link shows how to save and read the bitmap to the internal storage.

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
  • 1
    Uploading image needs a real file that needs to be saved to storage, you cannot upload image without saving it to the storage. This solution will help without saving the file to the external storage. – Khalid Taha Nov 20 '18 at 16:54
  • Please if this answer helps you, then mark it as correct answer. – Khalid Taha Nov 20 '18 at 16:59
  • hi many thanks for the response khalid. I'm still confused on where should i put saveToInternalStorage function into my code. Is it on onActivityResult after the camera took the picture? And how can i get a bitmap file format from the camera? – Chris Dennis Nov 22 '18 at 05:55
  • hi khalid, i have managed to upload the file successfully with your recommendation. Thanks you very much for your help. There is only one issue, the picture quality is really low, the picture is all blurry. And i think the default quality 100 is already the highest i think? Any idea how to work this around? – Chris Dennis Nov 22 '18 at 06:26
  • sorry it turns out i can only get the thumbnail of the image itself, i am still unable to get the full image – Chris Dennis Nov 22 '18 at 07:17
  • Hi, I will give you my help soon. – Khalid Taha Nov 24 '18 at 12:44