Load image to imageview directly using aws s3 without downloading to local storage in android

Viewed 3055

Iam loading images from the digital ocean spaces using aws s3 now its download the image to the local storage and then loading the image in image view but i need to load the image directly without downloading the image in local

My Current Code:

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 Answers

@karthikeyan comment helped me here is the answer:

    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);

load the image to image view using Picasso or Glide with the url

  1. You can use Picasso for loading image from a url. Here is an example -

Use Gradle In App Level:

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

Use this line to load image from your url -

Picasso.get().load("https://xxx.yyyy.com/your_image.png").into(imageView); // Your imageview
  1. Glide also a solution

Use 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 is also a powerful system for displaying images in Android Developed by facebook.

Enjoy android

Related