[英]Android: Error in retrieving image from uri
在我的Android應用程序中,用戶可以輸入個人資料圖片。 我的資產文件夾中有一個預定義的數據庫。 它有一個名為Person的表。 在“個人”表中,有一個具有BLOB類型的“個人資料圖片”字段。 我已經使用它的uri將個人資料圖片保存在數據庫中。 我已經使用以下代碼段來檢索圖像的uri。
String path = null;
path = Images.Media.insertImage(getContentResolver(), bitmap,
"title", null);
Uri imageUri = Uri.parse(path);
String uriString = imageUri.toString() ;
person.setProfilePic(uriString);
ContentValues values = new ContentValues();
values.put(COLUMN_PROFILE_PICTURE, person.getProfilePic());
這是我的實體類部分。
public class Person {
private int id;
private String profilePic;
private String name, date_of_birth, age, gender, bloodGrp;
......
public String getProfilePic() {
return profilePic;
}
public void setProfilePic(String imageInByte) {
this.profilePic = imageInByte;
}
我已經使用這種方法從數據庫中檢索了人數據。
public ArrayList<Person> getPersonList() {
ArrayList<Person> personList = new ArrayList<Person>();
String sql = "SELECT p.PersonName, p.ProfilePicture, p.DOB "
+ "FROM EMPerson p " + "ORDER BY p.PersonName ";
System.out.println(sql);
ArrayList<?> stringList = selectRecordsFromDB(sql, null);
for (int i = 0; i < stringList.size(); i++) {
ArrayList<?> arrayList = (ArrayList<?>) stringList.get(i);
ArrayList<?> list = arrayList;
Person person = new Person();
person.setName((String) list.get(0));
person.setProfilePic((String) list.get(1));
person.setDate_of_birth((String) list.get(2));
personList.add(person);
}
return personList;
}
我使用以下方法從網址獲取位圖。
public Bitmap getBitmap(String url) {
Log.d("getBitmap", "getBitmap");
Bitmap bm = null;
try {
URL aURL = new URL(url);
bm = BitmapFactory.decodeStream(aURL.openConnection().getInputStream());
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return bm;
}
我正在調用此getBitmap方法,並在圖像視圖中設置位圖,如下所示。
Bitmap image;
image = getBitmap(i.getProfilePic());
proPic.setImageBitmap(image);
這是我的CUstomAdapter類。
package my.easymedi.controller;
import java.util.ArrayList;
import my.easymedi.entity.Person;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
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 CustomAdapter extends ArrayAdapter<Person> {
private ArrayList<Person> lstPerson;
private Context my_context;
public CustomAdapter(Context context, int textViewResourceId,
ArrayList<Person> objects) {
super(context, textViewResourceId, objects);
this.lstPerson = objects;
my_context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
// assign the view we are converting to a local variable
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
Person i = lstPerson.get(position);
if (i != null) {
TextView personName = (TextView) v.findViewById(R.id.personName);
ImageView proPic = (ImageView) v.findViewById(R.id.imgProPic);
TextView dob = (TextView) v.findViewById(R.id.dateOfBirth);
if (personName != null) {
personName.setText(i.getName());
}
if (proPic != null) {
Bitmap image;
image = getBitmap(i.getProfilePic());
proPic.setImageBitmap(image);
}
if (dob != null) {
dob.setText(i.getDate_of_birth());
}
}
return v;
}
public Bitmap getBitmap(String url) {
Log.d("getBitmap", "getBitmap");
Bitmap bm = null;
try {
Uri aURL = null;// new URL(url);
Uri.parse(url);
bm = BitmapFactory.decodeStream(my_context.getContentResolver().openInputStream(aURL));
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return bm;
}
}
但是問題是圖像視圖為空。 即使log cat不顯示任何錯誤消息。 我正在用模擬器進行測試。 有人能這么仁慈地解釋這里發生了什么嗎?
提前致謝
而不是aURL.openConnection().getInputStream()
,應使用getContentResolver().openInputStream(aURL)
從內容URI檢索數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.