Android - Reference resource drawable as a URL - android

I'm using an API in my Android app that downloads images from the web and shows a placeholder animation while the images are downloading (RemoteImageView, part of the Ignition package here.
At some points in the app, I need to show images from the local resource drawables and not downloaded from the web.
Is it possible to access the local resource drawables using a URL ?

The way to do it:
use this line "android.resource://[your package]/"+[res id]
Here is an example
Uri myURI = Uri.parse("android.resource://com.example.project/" + R.drawable.myimage);
for convenient you can use it as a method:
public static Uri getUrl(int res){
return Uri.parse("android.resource://com.example.project/" + res);
}
Warning do not use context.getPackageName() as substitute to package name because it might return a null value.

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.

Related

Path to Local Image Specified in JSON

I'm working with a tutorial fromRay Wenderlich (https://www.raywenderlich.com/124438/android-listview-tutorial). In the tutorial, an image is retrieved via the Internet using a URL specified in the locally stored JSON file:
"image" : "https://www.edamam.com/web-img/341/3417c234dadb687c0d3a45345e86bff4.jpg"
The string is stored in the imageUrl variable:
recipe.imageUrl = recipes.getJSONObject(i).getString("image");
Then the image is loaded using Picasso:
Picasso.with(mContext).load(recipe.imageUrl).placeholder(R.mipmap
.ic_launcher).into(thumbnailImageView);
I would like to change the code in the tutorial so that the image is retrieved from the drawable folder within the app, rather than via the Internet. I've been working on this on and off for several days. I assumed it was a matter of changing the URL in the JSON data so that it specified a path to the image file in the drawable folder like this:
"image" : "android.resource://com.raywenderlich.alltherecipes/drawable/chicken.jpg"
So far I've been unsuccessful. Am I on the right path, or am I way off? I'm an Android newbie. I'm used to working with plists in Xcode (although I'm no expert there, either).

How to open google drive public link

I have uploaded a file on google drive, i have made it public. i am able to access this on browser but not with my android app.
I need to access this public link on MY ANDROID APP.
PFB the link of file:
https://drive.google.com/file/d/0B56C1LC3IVXCV055WFIyVTZjTTA/edit?usp=docslist_api
Is this possible to access a publicly shared GD file on android app?
Code snippet is
URL url = new URL("https://drive.google.com/file/d/0B56C1LC3IVXCV055WFIyVTZjTTA/edit?usp=docslist_api");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return bmp;
The solution is to format the address of the document you have shared.
Check this :
URL = "https://docs.google.com/uc?id=" + IDFile + "&export=download";
Your link is not to image but to Google Drive App with this image opened, real URL to image is this
Consider using Google Drive API
How about scaling the picture down to a reasonable size, it may just be that the image is too large for your heap to load
Bitmap.createScaledBitmap(Bitmap src, int Width, int Height, boolean filter)
There one other way to access file's public link By adding :
https://googledrive.com/host/[folderId]/fileName.extension
As you have provide file url
You can use that url like this for public access
https://googledrive.com/host/0B56C1LC3IVXCV055WFIyVTZjTTA
You simply need to grab the webContentLink of the newly created file.
From the documentation ...
webContentLink string
A link for downloading the content of the file in a browser using cookie based authentication. In cases where the content is shared publicly, the content can be downloaded without any credentials.

What is an image uri? Image uri inside the listview

Can someone please explain to me what's an image uri? I have an android app with a listview that can attach an image, and that listview displays the imag uri. thanks
URI is an address like: http://www.google.com/image.png it refers to the image somewhere.
It can also be a local address: file:////something.png
So you can attach a file that you don't have on your device and you don't want to download it.
URI or Uniform Resource Identifier is a compact sequence of characters that identifies an abstract or physical resource. It can be further classified as a locator, a name, or both.
Basically URI (in some cases URL or URN) will point to the image location, or will be the name of the image (why not both?).
Let's take a look at some URI examples:
https://stackoverflow.com/ (a URL because of the HTTPS protocol)
ftp://ftp.is.co.za/rfc/rfc1808.txt (also a URL because of the
FTP protocol)
http://www.ietf.org/rfc/rfc2396.txt (also a URL because of the
HTTP protocol)
ldap://[2001:db8::7]/c=GB?objectClass?one (also a URL because of the
protocol)
mailto:John.Doe#example.com
tel:+1-816-555-1212
telnet://192.0.2.16:80/ (also a URL because of the protocol)
Basically it's just a string in which identifies some-type of web resource.

How to use Universal image Loader for loading resources locally

I want to know how can i use nostra13 / Android-Universal-Image-Loader for displaying Images locally i.e from drawable folder along with the Memorycache. I want to use it with ViewPager.
any help will be greatly appreciated.
To load images from assets and drawables you should take ExtendedImageDownloader from example project (this class is not a part of library yet) and also set it to configuration.
UPD: Loading local resources (from drawable, assets, content provider) works out of the box since UIL v1.8.0.
See README:
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
NOTE: Use drawable:// only if you really need it! Always consider the native way to load drawables — ImageView.setImageResource(...) instead of using of ImageLoader.
Whenever More than one image load from resource dynamically (#runtime) than prefer these one:
String imgUri = "drawable://" + getResources().getIdentifier(imgName, "drawable", getActivity().getPackageName());
Here, imgName = Name of image in resource

Accessing an image outside the drawable folder in android

I have been using the images in the drawable folder to display images In my Android Application. Is there any way to access the images outside the drawable folder? Or images in another through a url?
The BitmapFactory class has different methods allowing you to decode resources from streams or files, you should check it out.
Just use PackageManager:
getDrawable (String packageName, int resid, ApplicationInfo appInfo)

Categories

Resources