Glide load local image by Uri.
While waiting for someone to respond, I tried to make a File
instance from the Uri
and load that. It works! Weird!
Glide.with(mContext)
.load(new File(pictureUri.getPath())) // Uri of the picture
.transform(new CircleTransform(..))
.into(profileAvatar);
How to load image from local storage using Glide
To get full path I used
val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).list()
?.get(0)
.toString()
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
will generate path Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).list()?.get(0)
get first image (if exists)
How to load Image into ImageView from Url using Glide v4.0.0RC1
If you are using Glide v4.0.0-RC1 then you need to use RequestOptions
to add the placeholder, error image and other option. Here is an working example
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher_round)
.error(R.mipmap.ic_launcher_round);
Glide.with(this).load(image_url).apply(options).into(imageView);
Can not load image from file path to ImageView with Glide
Just need to add android:requestLegacyExternalStorage="true"
to Application
tag in Manifest.xml
.
Display image from URI with content:// scheme with Glide on Android Q
Your getUriFromContentResolver()
is wrong. Query for the _ID
, then use ContentUris.withAppendedId()
to assemble the Uri
to use.
For example, in this sample app from this book, I use:
suspend fun getLocalUri(filename: String): Uri? =
withContext(Dispatchers.IO) {
val resolver = context.contentResolver
resolver.query(collection, PROJECTION, QUERY, arrayOf(filename), null)
?.use { cursor ->
if (cursor.count > 0) {
cursor.moveToFirst()
return@withContext ContentUris.withAppendedId(
collection,
cursor.getLong(0)
)
}
}
null
}
where PROJECTION
and QUERY
are:
private val PROJECTION = arrayOf(MediaStore.Video.Media._ID)
private const val QUERY = MediaStore.Video.Media.DISPLAY_NAME + " = ?"
and where collection
is:
private val collection =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) MediaStore.Video.Media.getContentUri(
MediaStore.VOLUME_EXTERNAL
) else MediaStore.Video.Media.EXTERNAL_CONTENT_URI
(in my case, I am querying videos; you would use MediaStore.Images
instead)
Glide not loading Image from File path
As discussed, here is the code to save the image at a particular path and also how to retrieve it. Also, I'm using Realm to save the path of the image, you can use anything else. Here is the code to save image:
private void saveToInternalStorage(Bitmap bitmapImage, final String fileName) {
ContextWrapper cw = new ContextWrapper(getContext());
directory = cw.getDir("myloImages", Context.MODE_PRIVATE);
if (!directory.exists()) {
directory.mkdir();
}
mypath = new File(directory, fileName + ".png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
Log.v("IMAGE PATH", mypath.getAbsolutePath());
} catch (Exception e) {
Log.e("SAVE_IMAGE", e.getMessage(), e);
}
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
PhotosRealm photosRealm = new PhotosRealm();
photosRealm.setImageLocation(directory.getAbsolutePath() + "/" + fileName + ".png");
realm.copyToRealm(photosRealm);
// I'm using realm to save location.
}
});
finish();
}
And now to retrieve the image from that location. It is same as your code:
private RealmResults<PhotosRealm> list;
Glide.with(context).load(list.get(0).getImageLocation()).asBitmap().into(imageView);
Using Glide for Android, how do I load images from asset and resources?
For resource ids, you can use:
Glide.with(fragment)
.load(R.drawable.resource_id)
.into(imageView);
For assets, you can construct an asset uri:
Glide.with(fragment)
.load(Uri.parse("file:///android_asset/<assetName>"))
.into(imageView);
Related Topics
Android Studio 4.1 Plugin Error: Plugin * Is Incompatible (Supported Only in Intellij Idea)
Push Up Content When Clicking in Edit Text
Rotate Zoom Drag Image in Android Imageview
Picasso Load Drawable Resources from Their Uri
How to Add Parameters to API (Http Post) Using Okhttp Library in Android
Suggestions to Avoid Bitmap Out of Memory Error
Android Studio 1.0.2 Not Building App:Mergedebugresources Error
Inflate a View in a Background Thread
Multiple Apps Use Same Content Provider
React-Native :Java.Lang.Unsatisfiedlinkerror: Couldn't Find Dso to Load: Libhermes.So
How to Get the Battery Level After Connect to the Ble Device
Cardview Inside Recyclerview Has Extra Margins
Okhttp Library - Networkonmainthreadexception on Simple Post
How to Get Chrome Browsing History/Bookmarks in Our Android App
5.1 Crash - a Taskdescription's Primary Color Should Be Opaque
Given a Tensor Flow Model Graph, How to Find the Input Node and Output Node Names