Get the URI of an image stored in drawable
You should use ContentResolver
to open resource URIs:
Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
InputStream stream = getContentResolver().openInputStream(uri);
Also you can open file and content URIs using this method.
Android - Reference resource drawable as a URL
Local image resources do not have urls, they have URIs. So if you have image in drawable, you can parse them from resource id to URI.
Uri uri=Uri.parse("R.drawable.image");
However, if you can also put your images in asset folder of the package and access them using their URL. The URL of the image files would be "file:///android_asset/image.png"
You can use either of the option.
How to get URI on imageview with Glide
to get Uri change ur Glide
implementation to:
Bitmap bitmap;
Glide.with(getApplicationContext())
.load(contentImageUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
// you can do something with loaded bitmap here
contentImage.setImageBitmap(resource);
bitmap= resource;
}
});
Now call the method;
//get URI from bitmap
Uri uri = getImageUri(getApplicationContext(),bitmap);
in getImageUri
function:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), inImage, UUID.randomUUID().toString() + ".png", "drawing");
return Uri.parse(path);
}
You need to add permission in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also for runtime permission: Api>=23
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Related Topics
What's Oncreate(Bundle Savedinstancestate)
Add "View More" at the End of Textview After 3 Lines
Android App Crashes When Launched in Debug Mode
Determine Addaction Click for Android Notifications
Sniffing/Logging Your Own Android Bluetooth Traffic
How to Set a Particular Font for a Button Text in Android
Android Edittext - Finished Typing Event
How to Use Custom Permissions in Android
How to Use Getsystemservice in a Non-Activity Class (Locationmanager)
Drawerlayout Double Drawer (Left and Right Drawers Simultaneously)
How to Create a Resizable Rectangle with User Touch Events on Android
Android: Android.Content.Res.Resources$Notfoundexception: String Resource Id #0X5
Manually Put Files to Android Emulator Sd Card
How to Pass a View's Onclick Event to Its Parent on Android
How to Add Window -- Token Android.Os.Binderproxy Is Not Valid; Is Your Activity Running
How to Open/Display Documents(.Pdf, .Doc) Without External App