I am developing an android app that needs to load many images from a folder that is located on my online server (in this case a GoDaddy hosting shared-plan).
All of the images are stored in an image folder inside the FileManager folder provided by GoDaddy.
Glide needs a URL to load an image, but in my case these images are not public and can't be reached from a HTTP URL (and should remain that way).
I would like to use glide to load these remotely stored images just like I am able to do so locally by providing a local path to the images on my local machine
For example this code works locally where path = (C:\Users\user\images\myImage.png) notice that it is not https:// url .
Glide.with(mContext)
.load(C:\Users\user\images\myImage.png)
.into(mImageView);
The path provided here is local and works on my local machine, I would like to replace localPath with remoteStorageFolderPath but I am unsure how it is done. Any help would be greatly appreciated!
Thank you.
so I think this has already been brought up as an issue in Glides Github, and was solved by TWiStErRob on 10 Nov 2016. The way to do it is to add an authorisation header as follows:
LazyHeaders auth = new LazyHeaders.Builder() // This can be cached in a field and reused later.
.addHeader("Authorization", new BasicAuthorization(username, password))
.build();
Glide
.with(context)
.load(new GlideUrl(url, auth)) // GlideUrl is created anyway so there's no extra objects allocated.
.into(imageView);
}
public class BasicAuthorization implements LazyHeaderFactory {
private final String username;
private final String password;
public BasicAuthorization(String username, String password) {
this.username = username;
this.password = password;
}
#Override
public String buildHeader() {
return "Basic " + Base64.encodeToString((username + ":" + password).getBytes(), Base64.NO_WRAP);
}
}
Related
I have uploaded few images in firebase storage, now i need the public http link of an image, not the download link which is like: com.google.android.gms.tasks.zzw#13xxxxx
I actually want a link that can pasted in the google search and show me that image like:
https://firebasestorage.googleapis.com/v0/b/pay-a6698.appspot.com/o/images%2Fname%2Fprofile?alt=media&token=b0745cdc-3078-42a9-a574-03539fa64e1f
how to do this?
I did it like this but this, it link in the form : com.google.android.gms.tasks.zzw#13xxxxx
private fun getImageUrl(filename: String)=CoroutineScope(Dispatchers.IO).launch{
var downloadUrl=imageRef.child("images/name/$filename").downloadUrl
imageUrl=downloadUrl.toString()
Log.d("url", imageUrl!!)
//saving url in sharedPref
val editor=sharedPrefCam.edit()
editor.apply {
putString("profileImgUrl", imageUrl)
apply()
}
}
i need the url pointed to in red
I am new to aws s3. I have some images in my aws s3 bucket. How can i retrieve the images and display it in android app.
If anybody knows please tell.
Your retrieval URL should look something like below considering that your bucket has no access restrictions.
String imagePath = https://s3.YOUR_BUCKET_REGION.amazonaws.com/YOUR_BUCKET_NAME/FILE_NAME.jpg
For example,
String imagePath = https://s3.eu-west-2.amazonaws.com/my_photos_bucket/my_pic.jpg
Use Glide or Picasso to load the image to your ImageView
You can use code similar to below to download a list of files in a bucket
AmazonS3Client s3 = create you AmazonS3Client
ObjectListing listing = s3.listObjects( bucketName, prefix );
List<S3ObjectSummary> summaries = listing.getObjectSummaries();
while (listing.isTruncated()) {
listing = s3.listNextBatchOfObjects (listing);
summaries.addAll (listing.getObjectSummaries());
}
for(S3ObjectSummary objectSummary : summaries) {
String key = objectSummary.getKey();
//download the file with object key = key
}
I need to download 20 images from remote server and store in Android folder.
Here my code:
Interface:
#GET
Call<ResponseBody> downloadFile(#Url String url);
In fragment:
private void downloadImagesList(List<Image> imagesList) {
for (Image image : imagesList) {
final String imageSourceUrl = imageSource.getUrl();
Call<ResponseBody> call = RestClientFactory.getRestClient().downloadFile(imageSourceUrl);
call.enqueue(new DefaultRestClientCallback<ResponseBody>() {
#Override
public void onSuccess(Response<ResponseBody> response) {
Toast.makeText(context, "Success download from url: " + imageSourceUrl, Toast.LENGTH_LONG).show();
// code to write downloaded image to Android's local folder
}
});
}
}
As you can see I iterate loop and call call.enqueue() for every image.
Is it good solution? Is this can perform my android app?
You can use dependencies like Glide , Pissaco etc for downloading images from remote server. Because these provide better caching and error handling. Have a look at the code below. It is using Pissaco to load the image
First add the Pissaco dependency in your build.gradle
compile 'com.squareup.picasso:picasso:2.5.2'
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
I am using Picasso for retrieving and showing images in my Android app. To avoid downloading all images over the network I am trying to add some images with the apk file, as sort of a pre-cached set of images. These images are stored in the assets folder and then copied to the Picasso cache folder on installation. This works as expected, but Picasso still download all images through the network and caches them as .0 and .1 files like this:
root#generic_x86:/data/data/com.my.app/files/images_cache #
ls
10.JPG
100.JPG
101.JPG
102.JPG
11.JPG
1f94664dec9a8c205b7dc50f8a6f3b79.0
1f94664dec9a8c205b7dc50f8a6f3b79.1
2.JPG
4621206beccad87a0fc01df2d080c644.0
4621206beccad87a0fc01df2d080c644.1
The *.JPG images are the ones I copied and the others are the Picasso cached images. Is there a way to make Picasso cache these images properly on installation?
If not, are there any other similar libraries that supports this kind of pre-caching?
Update: trying to cache from Assets folder
I tried making a small snippet that is run at first run of the app. The idea is to iterate the files in the given assets folder and fetch those images with Picasso. However, the below does not cache anything, although I end up in the onSuccess() method of the callback. The asset file names are correct. This is also verified by using the wrong folder name, which puts me in the onError() method of the callback.
I also tried loading it into a temporary ImageView, but it did do any difference.
public static boolean cacheImagesFromAssetsFolder(Context context)
{
boolean ok = false;
try
{
String[] images = context.getAssets().list("my_images");
for (String image : images)
{
Picasso.with(context).load("file:///android_asset/my_images/" + image).fetch(new Callback()
{
#Override
public void onSuccess()
{
// This is where I end up. Success, but nothing happens.
}
#Override
public void onError()
{
}
});
}
ok = true;
}
catch (Exception e)
{
e.printStackTrace();
}
return ok;
}
You could use File URI to request the Picasso to pick the image from your asset location instead of n/w.
Picasso.with(activity) //
.load(Uri.fromFile(file)) // Location of the image from asset folder
Update: How to use your own cache
import com.squareup.picasso.LruCache;
import com.squareup.picasso.Util;
LruCache imageCache = new LruCache(context);
Request request = Request.Builder(Uri.fromFile(asset_file), 0, null).build();
String cacheKey = Util.createKey(request, new StringBuilder());
imageCache.set(cacheKey, bitmap_object_of_asset_image);
Picasso.Builder(context)
.memoryCache(imageCache)
.build().load(asset_url).fetch(callback);
I am trying to load image from my S3 Bucket in my android application. My images are private so I won't be having any specific link for each image.
I'm using link generator,
s3Client.generatePresignedUrl(Constants.S3_BUCKET_NAME, key, expiration);
It generates a URL with let's say 1 hour or 2 min expiration.
Now I have problem in loading the url. I tried loading it by using picasso ,
Picasso.with(context).load(url.toString()).resize(30,38).into(holder.photo);
but it's not quite seems to be working. When I tried that link on browser I got following error
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
Try to attach error listener to Picasso and see what's going on. Also read logcat. Print URL which you pass to Picasso, does it correct?
Picasso.Builder builder = new Picasso.Builder(getApplicationContext());
builder.listener(new Picasso.Listener() {
#Override
public void onImageLoadFailed(Picasso arg0, String err) {
Log.e("Picasso Error", "Errored " + err);
}
});
builder.loggingEnabled(true);
Picasso pic = builder.build();
pic.load("image.jpg").into(iv);