How to retrieve image from MySQL using JSON in Android? - 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.

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>

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.

SQLite and MySQL

I'm working on an android application for my final year project. My app is about storing information and then display it out on the phone.
I figure it out by using sqlite, I'm able to store data in it, as well as retrieve it by using cursor c. But how am I able to make the retrieve data visible to the user?
example: I'm creating an app about Burger King, and I click the information about Burger King, it shows the information (when was is created etc) to the user.
This is kind of an open-ended question. If you want to present your search results to the user as a list or a grid, then use a ListView or a Gridview, and attach a CursorAdapter to your View. The CursorAdapter will read the results out of the database and provide them to the View. You'll probably want to subclass your CursorAdapter so that it generates the appropriate views for your ListView/GridView to display.
It's kind of an advanced topic, but I'm sure you can find tutorials and code samples.
If you just want to display a single data item on the screen (presumably a data item with a lot of detail), then you'll probably want to define a Dialog or Activity to present it to the user, and then read the data from the cursor and load it into the various View objects in your Dialog/Activity.
I hope this helps. It would be useful if we knew more detail about what you're trying to do.
I agree with Edward Falk on the need for more clearly defined details. In answering your question I would suggest creating some TextViews or a ListView in your xml file like so:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="#+id/viewl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/viewvaluesLabel"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/viewvaluesLabel"
android:textAppearance="?android:attr/textAppearanceLarge" />
.
.
.
</LinearLayout>
</ScrollView>
You can then get these TextViews by their respective ids using a "R.find" statement like:
TextView view1 = (TextView)findViewById(R.id.view1);
TextView view2 = (TextView)findViewById(R.id.view2);
TextView view3 = (TextView)findViewById(R.id.view3);
and then set the data you retrieve from your database table to display in these views using code like this:
Cursor c = db.getDetails(id);
if (c.moveToFirst()) {
view1.setText(c.getString(1));
view2.setText(c.getString(2));
view3.setText(c.getString(3));
} else {
db.close();
}
Hope that helps.

Upload images to server ImageSwitcher

I am a beginner in writing applications for Android.
I need an "ImageSwitcher" and populate it with images from a server queue. I've already researched a lot on the internet before coming to ask. Would anyone post some code samples or give some hint on what to search for?
What you're going to want to do it Create a view 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" />
<ImageSwitcher
android:id="#+id/image_switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
And then, behind the scenes in some sort of AsyncTask, download the images, and then display them. That code isn't too difficult.
This project on github may be of use as well. It seems to be good for lazy loading images in a listview, but I bet it could easily be modified to load a gallery.

Categories

Resources