【问题标题】:Android: Error in retrieving image from uriAndroid:从 uri 检索图像时出错
【发布时间】:2013-12-02 14:23:30
【问题描述】:

在我的 Android 应用程序中,用户可以输入个人资料图片。我的资产文件夹中有一个预定义的数据库。它有一个名为 Person 的表。在 Person 表中有一个字段 Profile Picture 具有 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;

}

我使用以下方法从 url 获取 Bitmap。

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 没有显示任何错误消息。我正在使用模拟器进行测试。谁能请好心解释一下这里发生了什么?

提前致谢

【问题讨论】:

    标签: java android bitmap


    【解决方案1】:

    您应该使用getContentResolver().openInputStream(aURL) 而不是aURL.openConnection().getInputStream() 从内容URI 中检索数据。

    【讨论】:

    • 我在从 ArrayAdapter 扩展的 CustomAdapter 类中使用了这个 getBitmap 方法。当我在这个 CustomAdapter 类中使用 getContentResolver() 时,它显示一个错误(方法 getContentResolver() 未定义为 CustomAdapter 类型)
    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2019-09-24
    相关资源
    最近更新 更多