使用AWS S3直接将图像加载到ImageView中,而无需下载到android中的本地存储

时间:2018-10-24 07:35:49

标签: java android amazon-web-services android-studio digital-ocean

我使用aws s3从数字海洋空间加载图像,现在将其下载图像到本地存储中,然后在图像视图中加载图像,但是我需要直接加载图像而无需在本地下载图像

我当前的代码:

AmazonS3Client s3;
    BasicAWSCredentials credentials;
    TransferUtility transferUtility;
    final TransferObserver observer,observers;
    String key = "XXXXXXXX";
    String secret = "XXXXXXXXXXXXX";

    credentials = new BasicAWSCredentials(key, secret);
    s3 = new AmazonS3Client(credentials);
    s3.setEndpoint("https://xxx.yyyy.com");

    transferUtility = new TransferUtility(s3, getApplicationContext());
    CannedAccessControlList filePermission = CannedAccessControlList.PublicRead;

    observer = transferUtility.download(
            "xxx",
            "xxxx/01.jpg",
            new File("/storage/emulated/0/camera 1/05.jpg")
    );

    observer.setTransferListener(new TransferListener() {
        @Override
        public void onStateChanged(int id, TransferState state) {
            if (state.COMPLETED.equals(observer.getState())) {
                Toast.makeText(getApplicationContext(), "Space Download completed !!", Toast.LENGTH_LONG).show();
                Bitmap bmp = BitmapFactory.decodeFile("/storage/emulated/0/camera 1/05.jpg");
                imgview.setImageBitmap(bmp);

            }
        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
        }

        @Override
        public void onError(int id, Exception ex) {

            Toast.makeText(getApplicationContext(), "Space Download error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

2 个答案:

答案 0 :(得分:4)

@karthikeyan的评论为我提供了答案:

    AmazonS3Client s3;
    BasicAWSCredentials credentials;

    String key = "xxxxx"; //SpacesKey
    String secret = "yyyyyyyyy"; //Spaces Secret key

    credentials = new BasicAWSCredentials(key, secret);
    s3 = new AmazonS3Client(credentials);

    s3.setEndpoint("https://RegionName.digitaloceanspaces.com"); //endpoint

    Date expires = new Date (new Date().getTime() + 1000 * 60); // 1 minute to expire
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey);  //generating the signatured url

    generatePresignedUrlRequest.setExpiration(expires);
    URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);

使用带有URL的Picasso或Glide将图像加载到图像视图中

答案 1 :(得分:2)

  1. 您可以使用毕加索从URL加载图像。 这是一个示例-

在应用程序级别使用Gradle:

implementation 'com.squareup.picasso:picasso:2.71828'

使用此行从您的网址中加载图片-

Picasso.get().load("https://xxx.yyyy.com/your_image.png").into(imageView); // Your imageview
  1. 滑行也是一种解决方案

使用Gradle:

repositories {
  mavenCentral() 
  google()
}

dependencies {
   implementation 'com.github.bumptech.glide:glide:4.7.1'
   annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}



// For a simple view:
  Glide.with(this).load("https://xxx.yyyy.com/your_image.png").into(imageView);
  1. Fresco还是一个功能强大的系统,用于在 facebook 开发的Android中显示图像。

享受android