Download Image from URI And Store it in the Folder - android

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>

Related

Volley NetworkImageView with spinner?

so I want to provide some feedback to the user while I am loading a gridview of images. Below is my adapters UI. I have a very nasty way of doing my task, adding a spinner to each element of the gridview. When the NetworkImageView loads it hides the spinner..
However I ask myself, do I want to use or show this to anyone? No way, aside from being a hack, having 8+ spinners is terrible for performance.
I've been researching it... I see there is some copyNpaste a custom version of the NetworkImageViewer code that lets me put an observer, but that seems to be way intense / overkill.
I also tried putting a callback into the get / set image in the volley ImageLoader to turn off the spinners when the data is accessed. It half worked, but decided it was too ugly of a solution as well.
Aside from just doing it manually with the normal volley request and having to process the image manually and putting it on the volley success listener.. I'm stumped. Anyone know of a clean solution here? I'm guessing I have to do it manually..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="#dimen/image_size"
android:paddingBottom="#dimen/element_spacing"
android:paddingTop="#dimen/element_spacing">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="#dimen/element_spacing"
android:background="#color/color_primary"
android:gravity="center_vertical" />
<ProgressBar
android:id="#+id/grid_adapter_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/gridview_adapter_networkimageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/element_spacing"
android:background="#color/color_primary"
android:layout_centerInParent="true"
/>
</RelativeLayout>

Android - App crashing due to memory error

I have an app that is not graphic intensive (as such). It has 2 Fragments, both with 2 GridViews inside. This is what I call Level-1 Activities because they are visible immediately when the app is opened.
Now the second GridView located inside Fragment #2 has 3 items, one of these Items opens a New Activity with a GridView inside it. This is what I call Level-2 Activities as they must be accessed by the user.
This second level GridView has 8 Image items. The Items are all identical size and shape, but the Images used for them are not, so they are scaled/resized etc. The Images are:
Image 1 - 465x232, 38kb
Image 2 - 512x251, 41kb
Image 3 - 900x379, 68kb
Image 4 - 630x258, 35kb
Image 5 - 700x346, 44kb
Image 6 - 615x409, 24kb
Image 7 - 800x383, 64kb
Image 8 - 400x169, 26kb
GridView XML
<GridView
android:id="#+id/essentials_gridview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:listSelector="#android:color/transparent"
android:numColumns="3"
android:padding="10dp"
android:stretchMode="columnWidth" />
GridView Item Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="125dp" >
<ImageView
android:id="#+list/grid_item_image_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />"
<TextView
android:id="#+list/grid_item_image_text"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:background="#50000000"
android:singleLine="true"
android:ellipsize="end" />
</RelativeLayout>
The Error I get is:
08-10 10:09:42.060: E/dalvikvm-heap(18982): Out of memory on a 9023376-byte allocation.
This happens when I'm moving back and forth between the Level 1 and Level 2 activities. After couple of times, it crashes with that error.
Seems you have a memory leak, I suggest you to watch this session:
Google I/O 2011: Memory management for Android Apps
In my own experience, I recommend you to implement ViewHolder pattern inside your Gridview's adapters.
I suspect the problem is related to image handling. You could try to use sampled version of the original if they are big. You could also cache the image that will be memory efficient. When you are navigating between the activities the images are loading again and again and consuming the memory.
The following link could be useful here. If you post your sample java code where you are loading image then it will be possible to pin point your issue.
http://developer.android.com/training/displaying-bitmaps/index.html
http://developer.android.com/training/displaying-bitmaps/manage-memory.html

how can i insert an image having a .gif format?

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 )

Display images stored in cache, Android

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.

How to retrieve image from MySQL using JSON in Android?

For my android application I want load image from remote MySQL database to the Gallery View using JSON.
Is it possible or not.
possible means any one help me with some sample code based on this
This is possible. First of all you need to get the image url saved in the remote server. That can be made possible by an api call either by using JSON or any other mode. By that api you can retrieve the image url's saved on the remote database. After getting all the image url's you can load the images using Lazy load of images in ListView. On the LazyLoading code they have used a ListView in the layout so the images and the corresponding text are shown as list items, you can change that ListView to a Gallery something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="#+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clear Cache"/>
</LinearLayout>
I think this will get you the desired result.

Categories

Resources