How to load image from aws with picasso with private access

后端 未结 4 1005
广开言路
广开言路 2021-02-09 00:11

I\'m trying to load image stored on aws S3 into my android app using Picasso but I am getting a blank image with no errors in my logcat and nothing to me from general debugging

相关标签:
4条回答
  • 2021-02-09 00:33

    You can use the below set of code , which is a aynctask which will use glide to attach private images from s3 by generating presignedurl.

      public class sets3imageasync extends AsyncTask<Object, Void, String> {
        // The list of objects we find in the S3 bucket
    
    static final String s3bucket=bucket_name;
    
    
    Context scontext;
    ImageView image;
    
    
    @Override
    protected String doInBackground(Object... objects) {
        // Queries files in the bucket from S3.
        Calendar cal = Calendar.getInstance();
        Log.d(TAG, "doInBackground: in progress");
        cal.setTime(new Date());
        cal.add(Calendar.HOUR, +1);
        Date oneHourLater = cal.getTime();
        AmazonS3Client s3 =  (AmazonS3Client) objects[0];
        scontext= (Context) objects[1];
        image=(ImageView) objects[2];
        String imagekey=(String) objects[3]; 
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(s3bucket, imagekey)
                        .withMethod(HttpMethod.GET)
                        .withExpiration(oneHourLater);
        URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);
    
        Log.e("url was", url.toString());
        return url.toString();
    }
    
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    
    
        Log.d(TAG, "onPostExecute: completed "+s);
    
        Glide.with(scontext).load(s)
                .apply(new RequestOptions()
                        .centerCrop())
                .placeholder(scontext.getResources().getDrawable(R.drawable.progress_animation))
                .into((image));
    
    }
    

    this can be called as below

    new sets3imageasync().execute(s3Client,context,Image_view);
    
    0 讨论(0)
  • 2021-02-09 00:40

    Simply use this:

    Picasso.with(getApplicationContext()).load(your_url).noFade().into(imageView);
    
    0 讨论(0)
  • 2021-02-09 00:41
    write below code to load image in Picasso. 
    variables:-  
    String file_path                          -->> this is your image file path 
    Imageview mViewHolder.img_post_photo      -->> this is your imageview to load image.
    
                            Picasso.with(context)
                                    .load(file_path)
                                    .placeholder(R.mipmap.ic_launcher)
                                    .error(R.mipmap.ic_launcher)
                                    .into(mViewHolder.img_post_photo, new Callback() {
                                        @Override
                                        public void onSuccess() {
    
                                        }
    
                                        @Override
                                        public void onError() {
                                            Picasso.with(context)
                                                    .load(file_path)
                                                    .placeholder(R.mipmap.ic_launcher)
                                                    .error(R.mipmap.ic_launcher)
                                                    .into(mViewHolder.img_post_photo);
                                        }
                                    });
    
    Set dependencies in your app build.gradle file:-
    compile 'com.squareup.picasso:picasso:2.5.2'
    
    hope this code helps you.
    
    0 讨论(0)
  • 2021-02-09 00:46

    You need to generate a presigned Url from S3 client and you can pass that url to picasso. That url will be public and will have an expriy date.

    0 讨论(0)
提交回复
热议问题