0

I am building an android app that allows users to take pictures. The pictures gets saved in /files/Pictures within the app. I am using the following code to upload images:

public void uploadFileToS3() {
        String ACCESS_KEY = "";
        String SECRET_KEY = "";
        String MY_BUCKET="";
        BasicAWSCredentials credentials = new BasicAWSCredentials( ACCESS_KEY, SECRET_KEY );
        AmazonS3 s3 = new AmazonS3Client( credentials,Region.getRegion( Regions.AP_SOUTH_1 ) );
        java.security.Security.setProperty("networkaddress.cache.ttl" , "60");
        s3.setEndpoint( "https://s3-ap-south-1.amazonaws.com" );
        TransferUtility transferUtility = TransferUtility.builder().s3Client(s3).context(getApplicationContext()).build();
        File storageDir = getExternalFilesDir( Environment.DIRECTORY_PICTURES);
        String files[] = storageDir.list();
        int fileCount = storageDir.listFiles().length;
        for (int i=0;i<=fileCount; i++) {
            File uploadToS3 = new File(files[i]);
            String file = files[i];
            TransferObserver transferObserver = transferUtility.upload( MY_BUCKET,file, uploadToS3);
            transferObserver.setTransferListener( new TransferListener() {
                @Override
                public void onStateChanged(int id, TransferState state) {
                    Toast.makeText( getApplicationContext(), "State Change:"+ state, Toast.LENGTH_SHORT ).show();
                }

                @Override
                public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                    int percentage = (int) (bytesCurrent/bytesTotal*100);
                    Toast.makeText( getApplicationContext(), "Upload Status : "+percentage+"%", Toast.LENGTH_SHORT ).show();
                }

                @Override
                public void onError(int id, Exception ex) {
                    Log.e("error","error");
                }
            } );
        }

    }

The problem I am facing is i am not able to upload the images to s3.

Edit 1:

I changed my code to this:

File storageDir = getExternalFilesDir( Environment.DIRECTORY_PICTURES);
        String files[] = storageDir.list();
        int fileCount = storageDir.listFiles().length;
        for (int i=0;i<=fileCount; i++) {
            String file = files[i];
            File uploadToS3 = new File(getApplicationContext().getFilesDir(),"Pictures/"+file);
            TransferObserver transferObserver = transferUtility.upload( MY_BUCKET,file, uploadToS3);

The rest remains the same. I am getting the following message in the logcat:

Caused by: java.lang.IllegalArgumentException: Invalid file: /data/user/0/com.example.syncsqlite/files/Pictures/ra_surf_surentp_ShelfPara_20191229_175752_1301070086.jpg

followed by:

at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:564)
        at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:530)
        at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:499)
        at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:443)
Apricot
  • 2,925
  • 5
  • 42
  • 88
  • If it's not even showing your toast, chances are your not even going into your for loop because your listFiles.length is returning 0 most likely. It's your filePath that might have an issue. You hard coded a file path which works on emulators but not on real devices. Look into how to use the getExternalFilesDir() method. Have a look at https://stackoverflow.com/questions/10123812/diff-between-getexternalfilesdir-and-getexternalstoragedirectory – ChallengeAccepted Dec 29 '19 at 06:53
  • @ChallengeAccepted Thank you...this is my first application...and I thought externaldir is for memory card...I will try this one and come back. – Apricot Dec 29 '19 at 09:01
  • @challengeaccepted Hi I have tried different options...but they are all getting failed. If I `Log` the `String file` I am able to see the exact file names with the above code. However, I think the uploadToS3 part of the transfer utility is failing....any thoughts. I also tried using `getAbsolutePath()` method on `File uploadToS3` assuming the uploadToS3 part is seeking the absolute path to the file...but it returns only the same file name the a `/` in front of the name. – Apricot Dec 29 '19 at 10:47
  • File handling can be a bit tricky on android. Where are you storing your images? Internal or external storage? I would recommend providing the code for how you store the images, so we can see how we can reference it. String pathToExternalStorage = Environment.getExternalStorageDirectory().toString(); File appDirectory = new File(pathToExternalStorage + "/" + "MyAppFolder"); File myPicture = new File(appDirectory, "myImage.jpg"); Make sure you have made the MyAppFolder directory and you're storing your images there. Please see: https://developer.android.com/training/data-storage – ChallengeAccepted Dec 30 '19 at 03:35
  • @challengeaccepted Thank you...I am storing it internally...I mean within the app. Will post the codes related to image storing soon... – Apricot Dec 30 '19 at 03:41
  • Please do not be confused by the term external storage on android. It is a bit misleading. There is Primary and Secondary external storage. Primary External Storage(which we usually just refer to as External Storage), is actually storage on the device that is accessible to the user and other apps. Meaning the folder and the content (i.e. images) you put there, can be accessed by the user and other apps. Internal storage means only your app can access it. The user can't retrieve the images/content you store on internal storage. https://www.freecodecamp.org/news/an-overview-of-android-storage/ – ChallengeAccepted Dec 30 '19 at 09:38

0 Answers0