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)