我是新來的android工作室。在這裏,我試圖從存儲在在線服務器上的mysql數據庫中獲取圖像,並在listvew上顯示圖像。但是我在自定義文件中出現錯誤。請幫助我。 this is the error i'm getting in log when i try to run my application從android數據庫的mysql數據庫檢索圖像
這是我的customJava代碼: 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;
}
}
這裏,是我的圖像列表瀏覽器的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);
}
}
}
這裏,圖像列表視圖的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>
有一些問題 「image_list_view.xml」。請張貼。 –
也添加了xml代碼...檢查並回復 –
image_list_view.xml文件有問題,它可能使用錯誤的標籤名稱。 – BobGao