文件路径的 AWS 下载功能问题
AWS Download function issue with file path
我已经使用以下代码实现了下载 aws s3 文件的功能:
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());
}
});
}
当我尝试执行以下代码时,出现以下错误:
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/
我无法将文件保存在外部存储设备上,因为我更喜欢将它们隐藏和保护起来,并在应用程序被删除时将其删除。我以前是用路径把文件保存在上面的,都是直接从网上加载的,没问题。
请大家指教。
提供给TransferUtility
的文件路径需要包含文件名,例如:
final String path = this.getFilesDir().getAbsolutePath() + "images-4.jpeg";
我终于找到了所问问题的解决方案,使用全新的、以前未使用过的路径很简单,因此它可以创建用于存储下载的图片或文件的文件夹。
final String path = this.getFilesDir().getAbsolutePath()+"/zip";
我希望它能对某人有所帮助,我希望亚马逊的反馈能比 'invalid file'.
更具体一点
我已经使用以下代码实现了下载 aws s3 文件的功能:
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());
}
});
}
当我尝试执行以下代码时,出现以下错误:
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/
我无法将文件保存在外部存储设备上,因为我更喜欢将它们隐藏和保护起来,并在应用程序被删除时将其删除。我以前是用路径把文件保存在上面的,都是直接从网上加载的,没问题。 请大家指教。
提供给TransferUtility
的文件路径需要包含文件名,例如:
final String path = this.getFilesDir().getAbsolutePath() + "images-4.jpeg";
我终于找到了所问问题的解决方案,使用全新的、以前未使用过的路径很简单,因此它可以创建用于存储下载的图片或文件的文件夹。
final String path = this.getFilesDir().getAbsolutePath()+"/zip";
我希望它能对某人有所帮助,我希望亚马逊的反馈能比 'invalid file'.
更具体一点