我使用适配器进行listview,我想从数据库访问String - >将其转换为URI,并将其放入输入流中 - >使用Bitmap factory解码输入流 - >将新位图设置为图像。
这是我的适配器代码:
public class CardViewAdapter extends BaseAdapter {
Context context;
List<Note> notes;
TextView cardTitleText;
TextView cardDescriptionText;
TextView cardDateText;
ImageView cardImage;
public CardViewAdapter(Context context, ArrayList<Note> notes) {
this.context = context;
this.notes = notes;
}
@Override
public int getCount() {
return notes.size();
}
@Override
public Object getItem(int position) {
return notes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.card_view, parent, false);
cardTitleText = (TextView) view.findViewById(R.id.titleTextCardView);
cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextCardView);
cardDateText = (TextView) view.findViewById(R.id.dateTextCardView);
cardImage = (ImageView) view.findViewById(R.id.cardImage);
Note note = notes.get(position);
cardTitleText.setText(note.getTitle());
cardDescriptionText.setText(note.getDescription());
cardDateText.setText(note.getDate());
if(note.getImagePath() != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(Uri.parse(note.getImagePath()));
databaseImage = BitmapFactory.decodeStream(inputStream);
cardImage.setImageBitmap(databaseImage);
inputStream.close();
} catch (IOException exception) { exception.printStackTrace(); }
}
return view;
}
}
这是我正在讨论并正在返回错误的部分
if(note.getImagePath() != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(Uri.parse(note.getImagePath()));
databaseImage = BitmapFactory.decodeStream(inputStream);
cardImage.setImageBitmap(databaseImage);
inputStream.close();
} catch (IOException exception) { exception.printStackTrace(); }
}
这是错误:
10-16 20:21:18.566: E/AndroidRuntime(11501): FATAL EXCEPTION: main
10-16 20:21:18.566: E/AndroidRuntime(11501): Process: com.meetz.personaljourney, PID: 11501
10-16 20:21:18.566: E/AndroidRuntime(11501): java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.contentprovider.MediaContentProvider from ProcessRecord{19ad093 11501:com.meetz.personaljourney/u0a102} (pid=11501, uid=10102) that is not exported from uid 10083
我在stackoverflow上读了这个错误的每一个线程,但我没有尝试过。我没有使用任何自定义提供程序,因此我的android_manifest.xml中没有提供程序。另外,我认为&#34; getContentResolver()&#34;是什么返回错误,但我不知道为什么。
------------------------编辑---------------------- -
以下是获取图像并将其存储在数据库中的代码:
我使用clicklistener为图像(占位符)转到图库并获取图像。 * AddNote只是我放置方法来清理代码的类。
addNote.getPopupImage().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
Uri uri = Uri.parse(path);
photoPickerIntent.setDataAndType(uri, "image/*");
startActivityForResult(photoPickerIntent, AddNote.IMAGE_GALLERY);
}
});
我正在寻找一个结果,如果我找到一个结果,我会使用类似的方法来检索照片。 * addNote.setPopupImage(selectedImage)使用所选图像更改占位符。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AddNote.IMAGE_GALLERY) {
Uri photoLocation = data.getData();
addNote.setPhotoPath(photoLocation.toString());
try {
InputStream inputStream = getContentResolver().openInputStream(photoLocation);
selectedImage = BitmapFactory.decodeStream(inputStream);
addNote.setPopupImage(selectedImage);
inputStream.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
这是我将标题,说明,日期和所选图像添加到列表和数据库的代码。我只是将URI作为字符串存储到数据库中,然后我将其检索并解析为URI(我在开头发布的代码的第一部分(适配器))
singleNote = new Note(popupTitle.getText().toString(),
popupDescription.getText().toString(),
new SimpleDateFormat("dd-MM-yyyy").format(new Date()),
photoPath != null ? photoPath : "");
notes.add(singleNote);
mainDatabase.addNewNote(singleNote);
如果有什么东西被误解,请询问,我会尽力帮助。