Sneha Srivastava Sneha Srivastava - 1 month ago 10
JSON Question

retrieve image from mysql database in android studios

I'm new to android studios. Here, I'm trying to fetch images from mysql database which is stored in a online server and displaying the image on listvew. But I'm getting error in my custom file. Please help me out. this is the error i'm getting in log when i try to run my application

this is my customJava code:
package com.example.manarpatel.book_app;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class CustomList extends ArrayAdapter<String> {
private String[] urls;
private Bitmap[] bitmaps;
private Activity context;

public CustomList(Activity context, String[] urls, Bitmap[] bitmaps) {
super(context, R.layout.image_list_view, urls);
this.context = context;
this.urls= urls;
this.bitmaps= bitmaps;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.image_list_view,parent, true);

TextView textViewURL = (TextView) listViewItem.findViewById(R.id.textViewURL);
ImageView image = (ImageView) listViewItem.findViewById(R.id.imageDownloaded);

textViewURL.setText(urls[position]);

Bitmap smaller = Bitmap.createScaledBitmap(bitmaps[position], 100, 50, false);
image.setImageBitmap(smaller);
// image.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position], 100, 50, false));

return listViewItem;
}
}


Here, is my imageList viewer java code:

package com.example.manarpatel.book_app;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import org.json.JSONException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class ImageListView extends AppCompatActivity implements AdapterView.OnItemClickListener {

private ListView listView;

public static final String GET_IMAGE_URL="http://usedbookapp.esy.es/getAllImages.php";

public GetAlImages getAlImages;

public static final String BITMAP_ID = "BITMAP_ID";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_list_view);

listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
getURLs();
}

private void getImages(){
class GetImages extends AsyncTask<Void,Void,Void>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(ImageListView.this,"Downloading images...","Please wait...",false,false);
}

@Override
protected void onPostExecute(Void v) {
super.onPostExecute(v);
loading.dismiss();
//Toast.makeText(ImageListView.this,"Success",Toast.LENGTH_LONG).show();
CustomList customList = new CustomList(ImageListView.this,GetAlImages.imageURLs,GetAlImages.bitmaps);
listView.setAdapter(customList);
}

@Override
protected Void doInBackground(Void... voids) {
try {
getAlImages.getAllImages();

} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
GetImages getImages = new GetImages();
getImages.execute();
}

private void getURLs() {
class GetURLs extends AsyncTask<String,Void,String>{
ProgressDialog loading;

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(ImageListView.this,"Loading...","Please Wait...",true,true);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
getAlImages = new GetAlImages(s);
getImages();
}

@Override
protected String doInBackground(String... strings) {
BufferedReader bufferedReader = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();

bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

String json;
while((json = bufferedReader.readLine())!= null){
sb.append(json+"\n");
}

return sb.toString().trim();

}catch(Exception e){
return null;
}
}
}
GetURLs gu = new GetURLs();
gu.execute(GET_IMAGE_URL);
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(this, ViewFullImage.class);
intent.putExtra(BITMAP_ID,i);
startActivity(intent);
}
}


java code to get all images:

package com.example.manarpatel.book_app;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class GetAlImages {

public static String[] imageURLs;
public static Bitmap[] bitmaps;

public static final String JSON_ARRAY="result";
public static final String IMAGE_URL = "url";
private String json;
private JSONArray urls;

public GetAlImages(String json){
this.json = json;
try {
JSONObject jsonObject = new JSONObject(json);
urls = jsonObject.getJSONArray(JSON_ARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}

private Bitmap getImage(JSONObject jo){
URL url = null;
Bitmap image = null;
try {
url = new URL(jo.getString(IMAGE_URL));
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return image;
}

public void getAllImages() throws JSONException {
bitmaps = new Bitmap[urls.length()];

imageURLs = new String[urls.length()];

for(int i=0;i<urls.length();i++){
imageURLs[i] = urls.getJSONObject(i).getString(IMAGE_URL);
JSONObject jsonObject = urls.getJSONObject(i);
bitmaps[i]=getImage(jsonObject);
}
}
}


here,the xml code of image list view

<LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="@+id/imageDownloaded" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_ic_menu_copy_mtrl_am_alpha"/>
<TextView android:id="@+id/textViewURL" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

</LinearLayout>

Answer

try to use Glide to retrive image from data base

public class NetworkImageView extends ImageView {

private String imageURL;

/**
 * Resource ID of the image to be used as a placeholder until the network image is loaded.
 */
private int mDefaultImageId;

/**
 * Resource ID of the image to be used if the network response fails.
 */
private int mErrorImageId;

private Bitmap bitmap;

public NetworkImageView(Context context) {
    super(context);
}

public NetworkImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public NetworkImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public String getImageURL() {
    return imageURL;
}

public void setImageUrl(String imageURL) {
    this.imageURL = imageURL;
    Glide.with(getContext())
            .load(imageURL)
            .override(200, 200)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(this);
}

/**
 * Sets the default image resource ID to be used for this view until the attempt to load it
 * completes.
 */
public void setDefaultImageResId(int defaultImage) {
    mDefaultImageId = defaultImage;
}

/**
 * Sets the error image resource ID to be used for this view in the event that the image
 * requested fails to load.
 */
public void setErrorImageResId(int errorImage) {
    mErrorImageId = errorImage;
}

public void setLocalImageBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
}

}

xml Lyaout :

<com.vynd.solution.utiles.NetworkImageView
                    android:id="@+id/backdrop"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/gradient"
                    android:scaleType="centerCrop"
                    android:src="@null" />

and use this

photo = (NetworkImageView) itemView.findViewById(R.id.photoItem);

photo.setImageUrl(url);

I hope to this exemple is useful More info Link

Comments