I download pictures from the Web and I stored them in the cache. Now, how I can display the photo in the XML. The photos stored as follwing:
/data/data/com.example.app/cache/wpta_i.jpeg
Changes according to the position. My XML is as following:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
How can I load the image from the cache and display it?
You should load the drawable from code and then setImageDrawable to the ImageView.
String pathName = "/data/data/com.example.app/cache/wpta_i.jpeg";
Drawable d = Drawable.createFromPath(pathName);
ImageView myImageView=(ImageView)findViewById(R.id.img);
myImageView.setImageDrawable(d);
You can use Context.getCacheDir() to get your cache directory and then access it from Code as shown in Perroloco's answer.
The documentation states a clear warning:
These files will be ones that get deleted first when the device runs low on storage.
There is no guarantee when these files will be deleted.
So make sure that your file exists prior to trying to load it.
Related
I'm getting a list from the service side with 10 image Uris. I was showing all those images in the Fragment Pages using Glide Library, but glide is taking time to load the image and show it in the UI. so I was planning to download all the images and store locally so that during each page I can show directly from the path and show it in the UI.
Kindly suggest me the way to download and show it from the Path.
Glide.with(getContext( )).load(ImageUri).into(ProductsImageView) - code used my be for Glide.
<LinearLayout
android:id="#+id/item_image_layout"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_below="#+id/header_text_layout"
android:layout_margin="20dp"
android:gravity="center">
<ImageView
android:id="#+id/products_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="#drawable/tomato"/>
</LinearLayout>
I am trying to bind an image file to an Android ImageView that is within an MvxListView. The images are just not appearing when the View is shown.
I have included the MvvmCross.HotTuna.Plugin.File package.
ListVideo.axml (snippet)
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_bright"
local:MvxBind="ImageUrl Thumbnail" />
The typical value for Thumbnail is
VideoData/2015-07-24-09-26-16/2fea9249-9370-4542-927a-c856e678f7b1.jpg
The value of Context.FilesDir.Path is /data/data/com.app.AppName/files
The image file /data/data/com.app.AppName/files/VideoData/2015-07-24-09-26-16/2fea9249-9370-4542-927a-c856e678f7b1.jpg does exist
I understand that to bind an image some custom folder determined within your app, I need to provide a path relative to Context.FilesDir.Path in order for the plugin to load it. I believe that the string in Thumbnail is correct.
But nothing is displayed in the ImageView. I have been banging my head against this one all morning. Any suggestions?
Edit :
I have managed to solve the problem !!
I needed the MvvmCross.HotTuna.Plugin.DownloadCache as well as the MvvmCross.HotTuna.Plugin.File package.
I have managed to solve the problem !!
I needed the MvvmCross.HotTuna.Plugin.DownloadCache as well as the MvvmCross.HotTuna.Plugin.File package.
Inside my AlertDialog I've an ImageView, when I choose to show the android:src="#drawable/logo_small" (a png around 64x64px in every folders /hdpi /ldpi ect..) all works fine. Instead when I show android:src="#drawable/logo_big" (it's just ONE png of 1417x1417pixel and 593KB in the drawable root folder) the image doesn't appear.
The layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageLogoBig"
android:layout_width="30dp"
android:layout_height="30dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/logo_big" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="#drawable/gplay_logo" />
</LinearLayout>
The imageLogoBig doesn't show in the upper example and in the lower example:
<ImageView
android:id="#+id/imageLogoBig"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/logo_big" />
I just discovered while I was writing the question that if I delete from drawable root directory the image logo_big and if I copy it in drawable-hdpi, drawable-ldpi, drawable-mdpi and drawable-xhdpi, all works fine. And this is strange because the other image: imageView1, is only in the drawable folder (but is 172x60px) and is showed correctly.
Why is this happening? Any suggest? (For example put logo_big only in drawable-xhdpi...)
I solved moving logo_big png image only to the directory drawable-xhdpi, don't know really why, but it works now.
This can be for a number of reasons, one is the name of the file not being compliant, that's clearly not your case but I'm pretty sure it's of many others like me that would reach this question when looking for an answer.
In a nutshell, underscores '_' are permitted but hyphens '-' are not, maybe because these are used to add resource qualifiers to file names (or at least directories).
This is something easy to remember, but even easier to forget. So, to put an example, this file didn't work:
poa-sample.png
And the solution was to rename it to:
poa_sample.png
I leave this here JFTR, because as stupid as this is, it a made me waste a lot of time and try everything else before. You'd expect Android Studio to give an error at build time, as it would do with an invalid variable identifier for example, but as of today it won't give you even a proper warning.
try renaming your image file to "logobig", without an underscore and see if that works. Even though file-based resource names allow underscores, i've had trouble occasionally
i'm having troubles with creating the first android app (Silent Toggle Mode from the pdf "Android.Application.Development.for.For.Dummies" ) with inserting an Imageview in the main.xml file, when I go to the graphical layout, the image isn't showing itself and this error appears:"The following classes could not be found:
- ImageView (Change to android.widget.ImageView, Fix Build Path, Edit XML)"
this is my xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="#string/hello_world"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<ImageView
android:id="#+id/phone_icon"
android:contentDescription="#string/description"
android:layout_width="wrap_content"
android:scaleType="fitXY"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/phone_on" />
<Button
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ToggleSilentMode" />
</LinearLayout>
thanks
This is one issue that I have also run across recently. It was sort of a misleading error to me and kept me stumped for around 30 minutes this morning. It isn't that Android cannot find the class. It is that it is having trouble importing the image source file.
Now the reason that it is having trouble finding the image is because I did not copy the source image into the directory correctly. I have chosen to drag the source file from another folder directory into the appropriate locations. In the process, it did not copy the file over properly. Or it copied it over as just a link to the other resource.
As a result Android does not know what to do with the resource and throws that odd error. In order to resolve this problem, you need to delete the old image sources and then replace them with direct full copies of their equivalents. Then you should see this error go away and the application working as expected from the xml layout code.
I faced the same problem once. To get it out, I replaced all the existing images, now it worked.
I had the same issue, when added a new ImageView in my xml with an image src.
I tried to open this image with Photoshop and get an "module format error". So the image was corrupted. The problem was solved creating a new image similar to the first one.
Hope it works :)
Can you give some projects how to insert an image like that on android?I want to add that image in my xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
How can I display it my image?
Use an ImageView. It takes all the standard formats.
You can use an ImageView:
<ImageView
android:id="#+id/logo_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/logo" />
and place your image file into res/drawable/logo.gif (or in a resolution specific drawable directory)
If you want to display moving GIF then there is not direct aproach, You have to try a way around, Refer bellow link for more details.
REF GIF in Android
OR you can load the image in a web View and display it. ( web view should be able to display the animation )