23
votes

I am trying to upload an image from Android directly to Google cloud storage. But the API does not seem to work. They have some Java samples which are tied to the App engine. I don't see any sample which is proven to work on Android.

On Android, I tried using the json api to upload an image. I am able to upload an image object but it seems to be corrupt. Moreover generating the authentication token seems to be tricky too.

I am struck with this right now. Did anyone on this earth ever tried uploading an image/video from Android using Java client or Json API and succeeded? Can someone point me in the right direction please. It has been very disappointing experience with this Storage api from Google. Please share your experiences if someone did it. Below is the code that I am trying from Android while trying to use the GCS's JSON API.

private static String uploadFile(RichMedia media) {
    DefaultHttpClient client = new DefaultHttpClient();
    Bitmap bitmap = BitmapUtils.getBitmap(media.getLocalUrl());
    HttpPost post = new HttpPost(GCS_ROOT + media.getLocalUrl() + "_" + System.currentTimeMillis());
    if(media.getType() == RichMedia.RichMediaType.PICTURE) {
        post.setHeader("Content-Type", "image/jpeg");
    } else {
        post.setHeader("Content-Type", "video/mp4");
    }
    post.setHeader("Authorization", "AIzaSyCzdmCMwiMzl6LD7R2obF0xSgnnx5rEfeI");
    //post.setHeader("Content-Length", String.valueOf(bitmap.getByteCount()));
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    try {
        post.setEntity(new StringEntity(new Gson().toJson(byteArray).toString()));
        HttpResponse response = client.execute(post);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String eachLine = null;
        StringBuilder builder = new StringBuilder();
        while ((eachLine = reader.readLine()) != null) {
            builder.append(eachLine);
        }
        L.d("response = " + builder.toString());
        JSONObject object = new JSONObject(builder.toString());
        String name = object.getString("name");
        return  name;
    } catch (IOException e) {
        L.print(e);
    } catch (JSONException e) {
        L.print(e);
    }
    return null;
}

I am running into two issues here.

  1. The file which got uploaded to the server is corrupt. It is not the same image that I uploaded. It is currupt.

  2. The authorization key expires very often. In my case, I am using the auth code generated by gsutil.

6
Yes, this should work. Could you please share your code and elaborate on what you mean by "corrupt"?jterrace
Added some code and the specific issues that I am running into.Gopinath
Why aren't you using the google-api-java-client ?jterrace
I did not find any sample piece of code which is using google api java client. I am not familiar with that piece. Can you help me point in the right direction?Gopinath
The answer to that question can be found here: [using-google-cloud-storage-json-api-in-android][1] [1]: stackoverflow.com/questions/21953744/…chelo_c

6 Answers

16
votes

Since no one is answering this question, let me update the way I solved this problem. I ended up following this https://github.com/pliablematter/simple-cloud-storage project.

I could upload Pictures/Videos to GCS from my Android app.

8
votes

Fixed for Android:

Android Studio config:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/android-support-v4.jar')
    compile files('google-play-services.jar')
    compile 'com.wu-man:android-oauth-client:0.0.3'
    compile 'com.google.apis:google-api-services-storage:v1-rev17-1.19.0'
    compile(group: 'com.google.api-client', name: 'google-api-client', version:'1.19.0'){
        exclude(group: 'com.google.guava', module: 'guava-jdk5')
    }
}

AndroidManifiest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

Main implementation:

    new AsyncTask(){

        @Override
        protected Object doInBackground(Object[] params) {
            try {

                CloudStorage.uploadFile("bucket-xxx", "photo.jpg");

            } catch (Exception e) {
                if(DEBUG)Log.d(TAG, "Exception: "+e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
    }.execute();

CloudStorage Class:

import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.StorageObject;

public static void uploadFile(String bucketName, String filePath)throws Exception {

    Storage storage = getStorage();
    StorageObject object = new StorageObject();
    object.setBucket(bucketName);
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,filePath);

    InputStream stream = new FileInputStream(file);

    try {
        String contentType = URLConnection.guessContentTypeFromStream(stream);
        InputStreamContent content = new InputStreamContent(contentType,stream);

        Storage.Objects.Insert insert = storage.objects().insert(bucketName, null, content);
        insert.setName(file.getName());
        insert.execute();

    } finally {
        stream.close();
    }
}

private static Storage getStorage() throws Exception {

    if (storage == null) {
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        List<String> scopes = new ArrayList<String>();
        scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);

        Credential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(ACCOUNT_ID_PROPERTY) //Email                           
                .setServiceAccountPrivateKeyFromP12File(getTempPkc12File())
                .setServiceAccountScopes(scopes).build();

        storage = new Storage.Builder(httpTransport, jsonFactory,
            credential).setApplicationName(APPLICATION_NAME_PROPERTY)
            .build();
    }

    return storage;
}

private static File getTempPkc12File() throws IOException {
    // xxx.p12 export from google API console
    InputStream pkc12Stream = AppData.getInstance().getAssets().open("xxx.p12");
    File tempPkc12File = File.createTempFile("temp_pkc12_file", "p12");
    OutputStream tempFileStream = new FileOutputStream(tempPkc12File);

    int read = 0;
    byte[] bytes = new byte[1024];
    while ((read = pkc12Stream.read(bytes)) != -1) {
        tempFileStream.write(bytes, 0, read);
    }
    return tempPkc12File;
}
3
votes

This snippet of code works for me great for uploading files from Android directly to GCS.

File file = new File(Environment.getExternalStorageDirectory(), fileName);

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        FileBody filebody = new FileBody(file,ContentType.create(mimeType), file.getName());

        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();        
        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("file", filebody);
        httppost.setEntity(multipartEntity.build());
        System.out.println( "executing request " + httppost.getRequestLine( ) );
        try {
            HttpResponse response = httpclient.execute( httppost );
            Log.i("response", response.getStatusLine().toString());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        httpclient.getConnectionManager( ).shutdown( );

MultipartEntityBuilder class is not included into android standard libraries so you need to download httpclient and include into your project.

2
votes

Hpsaturn's answer worked for me. He missed to answer a few points. How to get service account id and p12 file. For getting these 2, open console.developers.google.com and choose your project. Enable Cloud Storage API. You see a message to create credentials. Go to credentials in API manager and create credential by selecting Service account key and follow the details in image. You will get the service account id and p12 file from this screen.

enter image description here

Hpsaturn also missed to mention AppData, which is your custom Application class defined in manifest. For everyone's convenience, I am attaching the complete CloudStorage class here.

package com.abc.xyz.utils;

import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.abc.xyz.app.AppController;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.StorageObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wjose on 8/20/2016.
 */
public class CloudStorage {

    private static final String TAG = "CloudStorage";

    public static void uploadFile(String bucketName, String name, Uri uri)throws Exception {

        Storage storage = getStorage();
        StorageObject object = new StorageObject();
        object.setBucket(bucketName);
        File sdcard = Environment.getExternalStorageDirectory();
        //File file = new File(sdcard,filePath);
        File file = new File(uri.getPath());

        InputStream stream = new FileInputStream(file);

        try {
            String contentType = URLConnection.guessContentTypeFromStream(stream);
            InputStreamContent content = new InputStreamContent(contentType,stream);

            Storage.Objects.Insert insert = storage.objects().insert(bucketName, null, content);
            insert.setName(name);
            StorageObject obj = insert.execute();
            Log.d(TAG, obj.getSelfLink());
        } finally {
            stream.close();
        }
    }

    static Storage storage = null;
    private static Storage getStorage() throws Exception {

        if (storage == null) {
            HttpTransport httpTransport = new NetHttpTransport();
            JsonFactory jsonFactory = new JacksonFactory();
            List<String> scopes = new ArrayList<String>();
            scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);

            Credential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .setServiceAccountId("[email protected]") //Email
                    .setServiceAccountPrivateKeyFromP12File(getTempPkc12File())
                    .setServiceAccountScopes(scopes).build();

            storage = new Storage.Builder(httpTransport, jsonFactory,
                    credential).setApplicationName("MyApp")
                    .build();
        }

        return storage;
    }

    private static File getTempPkc12File() throws IOException {
        // xxx.p12 export from google API console
        InputStream pkc12Stream = MyApplication.getInstance().getAssets().open("xxxyyyzzz-0c80eed2e8aa.p12");
        File tempPkc12File = File.createTempFile("temp_pkc12_file", "p12");
        OutputStream tempFileStream = new FileOutputStream(tempPkc12File);
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = pkc12Stream.read(bytes)) != -1) {
            tempFileStream.write(bytes, 0, read);
        }
        return tempPkc12File;
    }
}

btb, I used only following dependency in the gradle.

compile 'com.google.apis:google-api-services-storage:+'

1
votes

I have tried all the above answers and none of them worked for me straight out of the box. Here is what i have done to make it working(only by editing from the above comments):

package  Your page name;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.StorageObject;

import java.io.File;
import java.io.*;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

 public class CloudStorage {

static Activity activity=null;
//http://stackoverflow.com/questions/18002293/uploading-image-from-android-to-gcs

static Storage storage=null;
public static void uploadFile(Activity activity2,String bucketName, String filePath)
{
    activity=activity2;
    try {
        Storage storage = getStorage();
        StorageObject object = new StorageObject();
        object.setBucket(bucketName);
        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(filePath);

        InputStream stream = new FileInputStream(file);

        try {
            Log.d("Alkamli","Test");
            String contentType = URLConnection.guessContentTypeFromStream(stream);
            InputStreamContent content = new InputStreamContent(contentType, stream);

            Storage.Objects.Insert insert = storage.objects().insert(bucketName, null, content);
            insert.setName(file.getName());
            insert.execute();

        } finally {
            stream.close();
        }
    }catch(Exception e)
    {
        class Local {}; Log.d("Alkamli","Sub: "+Local.class.getEnclosingMethod().getName()+" Error code: "+e.getMessage());

        e.printStackTrace();
    }
}

private static Storage getStorage() {

    try {

        if (storage == null)
        {
            HttpTransport httpTransport = new NetHttpTransport();
            JsonFactory jsonFactory = new JacksonFactory();
            List<String> scopes = new ArrayList<String>();
            scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);

            Credential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .setServiceAccountId("Service-Email-Address") //Email
                    .setServiceAccountPrivateKeyFromP12File(getTempPkc12File())
                    .setServiceAccountScopes(scopes).build();

            storage = new Storage.Builder(httpTransport, jsonFactory,
                    credential)
                    .build();
        }

        return storage;
    }catch(Exception e)
    {
        class Local {}; Log.d("Alkamli","Sub: "+Local.class.getEnclosingMethod().getName()+" Error code: "+e.getMessage());

    }
    Log.d("Alkamli","Storage object is null ");
    return null;
}

private static File getTempPkc12File() {
    try {
        // xxx.p12 export from google API console
        InputStream pkc12Stream = activity.getResources().getAssets().open("Service-key.p12");
        File tempPkc12File = File.createTempFile("temp_pkc12_file", "p12");
        OutputStream tempFileStream = new FileOutputStream(tempPkc12File);
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = pkc12Stream.read(bytes)) != -1) {
            tempFileStream.write(bytes, 0, read);
        }
        return tempPkc12File;
    }catch(Exception e)
    {
        class Local {}; Log.d("Alkamli","Sub: "+Local.class.getEnclosingMethod().getName()+" Error code: "+e.getMessage());

    }
    Log.d("Alkamli"," getTempPkc12File is null");
    return null;
}
}

I only edited few lines to make it work for me and for the dependencies in gradle You will need only these three. (Keep in mind that if you use all google depenacnes that may damage your whole project. in my case some of android's functions wouldn't work anymore )

    compile 'com.google.api-client:google-api-client:1.20.0'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.20.0'
    compile 'com.google.apis:google-api-services-storage:v1-rev17-1.19.0' 

The full project for people needing it : https://github.com/Fahad-alkamli/Chat-app

1
votes

Not so Surprisingly, none of the solutions above worked for me. The REASON, well in all the above answers they're using the .P12 file, while I got a JSON file as an api key. There are these two different keys which I didn't know at first and I thought I was doing wrong and after one intense day of going through google docs, stackoverflow I was finally able to upload to the storage. None of the above answers show on how to do it for a JSON file and the code is tad bit simple so I'm posting this answers, hope this helps someone:

UploadFile.java

public class UploadFile {

    public static Storage setCredentials(InputStream credentialFile) {
        InputStream credentialsStream = null;;
        Credentials credentials = null;
        try {
            credentialsStream = credentialFile;
            credentials = GoogleCredentials.fromStream(credentialsStream);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return StorageOptions.newBuilder()
                .setProjectId("YOUR_PROJECT_ID").setCredentials(credentials)
                .build().getService();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String transmitImageFile(Storage storage, String srcFileName, String newName) {
        File file = new File(srcFileName);
        byte[] fileContent = null;
        try {
            fileContent = Files.readAllBytes(file.toPath());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        if (fileContent == null)
            return null;
        if (fileContent.length == 0)
            return null;
        BlobInfo.Builder newBuilder = Blob.newBuilder(BucketInfo.of("YOUR_BUCKET_NAME"), newName);
        BlobInfo blobInfo = newBuilder.setContentType("image/png").build();
        Blob blob = storage.create(blobInfo, fileContent);
        String bucket = blob.getBucket();
        String contentType = blob.getContentType();
        Log.e("TAG", "transmitImageFile: "+contentType);
        System.out.println("File " + srcFileName + " uploaded to bucket " + bucket + " as " + newName);
        return newName;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private String currentPhotoPath;
    private String imageName;
    public static final int REQUEST_IMAGE_CAPTURE = 1;
    private File photoFile = null;
    private String[] permissions;
    public static final int PERMISSION_REQ_CODE = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView= findViewById(R.id.textView);
        permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_COARSE_LOCATION};
        PermissionsRequest();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dispatchTakePictureIntent();
                ReadFromAsset();
            }
        });
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try {
            photoFile = createImageFile();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this, getPackageName(), photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        String fileName = "temp";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName, ".jpg");

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        imageName = image.getName();

        return image;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Storage storage = UploadFile.setCredentials(getAssets().open("GoogleMapDemo.json"));
                        UploadFile.transmitImageFile(storage, currentPhotoPath, "sampleImage.jpg");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            Log.e("TAG", "ImagePath: " + currentPhotoPath);
            Log.e("TAG", "ImageName: " + imageName);
        }
    }

    private void PermissionsRequest() {
        if (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[0]) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getApplicationContext(), permissions[4]) != PackageManager.PERMISSION_GRANTED) {
            AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
            builder1.setTitle("AAtten");
            builder1.setMessage("Permissions");
            builder1.setCancelable(false);
            builder1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    acceptPermissions();
                }
            });
            builder1.setNegativeButton("SAIR", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            //Creating dialog box
            AlertDialog alert1 = builder1.create();
            alert1.show();
        }
    }

    private void acceptPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[0]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[1]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[2]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[3]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[4]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[5]) != PackageManager.PERMISSION_GRANTED)
                requestPermissions(permissions, PERMISSION_REQ_CODE);
            else {
                if ((ContextCompat.checkSelfPermission(getApplicationContext(), permissions[0]) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[1]) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[2]) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[3]) != PackageManager.PERMISSION_GRANTED) || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[4]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[5]) != PackageManager.PERMISSION_GRANTED)
                    requestPermissions(permissions, PERMISSION_REQ_CODE);
            }
        }
    }

    private void ReadFromAsset(){
        String string = "";
        try {
            //InputStream inputStream = new FileInputStream(String.valueOf(getAssets().open("key.p12")));
            InputStream inputStream = getAssets().open("GoogleMapDemo.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            string = new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.e("TAG", "ReadFromAsset: "+string );
    }
}

Now google on how to create a asset folder in the app and add the json key file in that folder. In onActivityResult class you'll pass the name of the json file.

And in UploadImage class give your projectID and bucketName in respective feilds. You can find the projectID in that json.file

Dependencies

android{
 packagingOptions{
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/DEPENDENCIES'
    }
}

    implementation platform('com.google.cloud:libraries-bom:16.2.1')
    implementation 'com.google.cloud:google-cloud-storage'
    implementation 'com.google.cloud:google-cloud-core:1.94.0'

Maifest

 <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">

<application
  <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.personalgooglestoragecheck"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
</application

file-path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path
        name="my_images"
        path="/" />
    <external-path
        name="external"
        path="." />

    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>

Hope this helps someone. Any questions feel free to ask.

EDIT 1:

Apparently Files.readAllBytes() is introduced in Java 7 and only works on Android api 26 or above. If you want to target lower versions use fileinputstream and remove all the @requireannotation tags.