java - 从android studio中的mysql数据库检索图像

标签 java android mysql json android-studio

我是 Android 工作室的新手。在这里,我尝试从存储在在线服务器中的 mysql 数据库中获取图像并在 listvew 上显示图像。但我的自定义文件中出现错误。请帮帮我。 this is the error i'm getting in log when i try to run my application

这是我的自定义Java代码: 包 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;
    }
}

这里是我的 imageList 查看器 java 代码:

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代码:

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);
        }
    }
}

这里是图片 ListView 的xml代码

<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>

最佳答案

尝试使用Glide从数据库中检索图像

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 布局:

<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" />

并使用这个

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

photo.setImageUrl(url);

我希望这个例子有用 更多信息Link

关于java - 从android studio中的mysql数据库检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36269091/

相关文章:

java - JmDNS 样本

java - Maven 缺少 Artifact org.geotools :gt-shapefile:jar:11-SNAPSHOT

android - 在 Android 应用程序中访问本地主机

Java 处理错误和异常

php - 如何在 Laravel 中将时间作为输入?

java - 递归神秘法——有人可以给我解释一下吗?

android - Cordova 应用程序接收从其他应用程序共享的内容 - iOS、Android 和 Windows 应用商店应用程序

mysql索引问题-解释说文件排序

MySQL - 仅获取特定 parent 的 child

java - 我应该如何将公钥转换为Java中的证书