【问题标题】:Amazon s3 file download from android从 android 下载 Amazon s3 文件
【发布时间】:2014-11-28 05:12:38
【问题描述】:

我正在尝试制作一个演示应用程序。

将图像从我的 s3 服务器下载到我的手机内存卡。

我尝试了演示代码并编写了以下内容。但是,一旦我在手机上运行它,应用程序就会关闭。 任何帮助将不胜感激。

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
File f=null;    
    try{
    File dir= Environment.getExternalStorageDirectory();
 f= new File(dir,"test.jpg");
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT).show();            
    }
    AWSCredentials creden=new BasicAWSCredentials(accessKey,secretKey);
    AmazonS3Client s3Client=new AmazonS3Client(creden);

    ObjectMetadata obj= s3Client.getObject(new GetObjectRequest("adj-temp","funflick_1.jpg"),f);


}

【问题讨论】:

  • 请发布与强制关闭相关的 logcat 输出,以便我们查看确切的异常详细信息?
  • 没有错误很难说。可能是多件事.. 缺乏对外部存储的权限和主线程上的网络是我通过查看代码可以得出的最佳猜测
  • 还要检查读写sdcard的权限!!

标签: android amazon-web-services amazon-s3


【解决方案1】:

试试这个它会工作快乐编码

{
        String str_FilePathInDevice = "/sdcard/" + "/"
                + "RestoreFolderName" + "/" + "filname.extention";

        File file = new File(str_FilePathInDevice);

        String str_Path = file.getPath().replace(file.getName(), "");
        File filedir = new File(str_Path);

        try {
            filedir.mkdirs();
        } catch (Exception ex1) {
        }
        S3Object object = s3Client.getObject(new GetObjectRequest(
                "BucketName", "keyName"));

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                object.getObjectContent()));
        Writer writer = new OutputStreamWriter(new FileOutputStream(file));

        while (true) {
            String line = reader.readLine();
            if (line == null)
                break;
            writer.write(line + "\n");
        }
    writer.flush();
    writer.close();
    reader.close();
}

【讨论】:

    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2012-08-29
    • 2013-07-29
    • 2013-05-08
    • 1970-01-01
    相关资源
    最近更新 更多