简体   繁体   English

Android:从uri检索图像时出错

[英]Android: Error in retrieving image from uri

In my Android application user can enter a profile picture. 在我的Android应用程序中,用户可以输入个人资料图片。 I have a predefined database in my assets folder. 我的资产文件夹中有一个预定义的数据库。 It has a table named Person. 它有一个名为Person的表。 In the Person table there is a field Profile Picture which has BLOB type. 在“个人”表中,有一个具有BLOB类型的“个人资料图片”字段。 I've saved the profile picture in the database using its' uri. 我已经使用它的uri将个人资料图片保存在数据库中。 I've used following code segment for retrieving uri of the image. 我已经使用以下代码段来检索图像的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());

This is my entity class segment. 这是我的实体类部分。

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

I've retrieved person data from database by using this method. 我已经使用这种方法从数据库中检索了人数据。

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;

}

I've used following method to get Bitmap from the url. 我使用以下方法从网址获取位图。

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

I am calling this getBitmap method and set the bitmap in an image view as following. 我正在调用此getBitmap方法,并在图像视图中设置位图,如下所示。

Bitmap image;
image = getBitmap(i.getProfilePic());
proPic.setImageBitmap(image);

This is my CUstomAdapter class. 这是我的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;
}

} }

But the problem is Image view is empty. 但是问题是图像视图为空。 Even though log cat doesn't show any error message. 即使log cat不显示任何错误消息。 I am testing with an emulator. 我正在用模拟器进行测试。 Can anyone plz be so kind enough to explain what's going on here ? 有人能这么仁慈地解释这里发生了什么吗?

Thanks in advance 提前致谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM