AWS Download function issue with file path

456 views Asked by At

I have implemented a function to download aws s3 files using the following code:

public void credentialsProvider()
{
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
            getApplicationContext(), "us-east-2:xxxxxx-xxxxx-xxxxx-xxxx-xxxxxxx", Regions.US_EAST_2
    );
   setAmazonS3Client(credentialsProvider);
    System.out.println("setAmazonS3Client done");
}

public void setAmazonS3Client( CognitoCachingCredentialsProvider credentialsProvider)
{
    s3 = new AmazonS3Client(credentialsProvider);

    s3.setRegion(Region.getRegion(Regions.US_EAST_2));
}

public void setTransferUtility()
{
    transferUtility = new TransferUtility(s3, getApplicationContext());
    System.out.println("setTransferUtility done");
}

public void setFileDownload()
{
    final String path = this.getFilesDir().getAbsolutePath();
    File myFile = new File(path);
    TransferObserver transferObserver = transferUtility.download("sample-bucket-001", "images-4.jpeg", myFile);
    transferObserverListener(transferObserver);
}

public void transferObserverListener(TransferObserver transferObserver)
{
    transferObserver.setTransferListener(new TransferListener() {
        @Override
        public void onStateChanged(int id, TransferState state) {
            System.out.println("onStateChanged: "+ state);
            if(state == TransferState.FAILED || state == TransferState.WAITING_FOR_NETWORK){
                
            }
        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            int percentage = (int)(bytesCurrent/bytesTotal * 100);
            System.out.println("percentage: "+ percentage);
        }

        @Override
        public void onError(int id, Exception ex) {
            System.out.println("Error faced: "+ ex.toString());
        }
    });
} 

As I try to execute the following code I get the following error:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxx.xxxxxxxx/com.xxxxxx.xxxxxxxx.Activity}: java.lang.IllegalArgumentException: Invalid file: /data/user/0/com.xxxxxx.xxxxxxxx/files/

I cannot save the file on external storage as I prefer to have them hidden and protected, and deleted in case the app got deleted. I used to use the path to save files on it, that are directly loaded from the internet and no problem there. Kindly advise on the matter.

2

There are 2 answers

0
coder On BEST ANSWER

I have finally found the solution to the question asked, which simple using a path that is totally new, unused previously, so it can create the folder that where it will store the downloaded picture or file.

final String path = this.getFilesDir().getAbsolutePath()+"/zip";

I wish Amazon's feedback could be a bit more specific than just 'invalid file'.

1
marthursson On

The file path supplied to TransferUtility needs to include a file name, e.g.:

final String path = this.getFilesDir().getAbsolutePath() + "images-4.jpeg";