项目:在一个活动中从一个按钮打开画廊,从那里选择“图像”,然后将其显示在初始“活动”屏幕上。
解决方案:我知道已经为上述问题提供了解决方案,但它只处理图像的多重选择部分并获取图像数据。
我的问题:
ImageView imageView = findViewById(R.id.imageView);
位图位图= BitmapFactory.decodeFile(imageUri.getPath());
imageView.setImageBitmap(bitmap);但图片没有显示
我是如何尝试获取位图的 位图位图= MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
我在下面的getBitmap部分遇到了Unhandled exceptions error,java.io.FileNotFoundException
我在下面附加了我的代码
public void onClick(View view){
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_MULTIPLE) {
if(resultCode == Activity.RESULT_OK) {
if(data.getClipData() != null) {
int count = data.getClipData().getItemCount(); //evaluate the count before the for loop --- otherwise, the count is evaluated every loop.
for (int i = 0; i < count; i++){
Uri imageUri = data.getClipData().getItemAt(i).getUri();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri) ; //MAIN ERROR OVER HERE
//OR
ImageView imageView = findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()) ;
imageView.setImageBitmap(bitmap); //STILL NOT WORKING
}
}
} else if(data.getData() != null) {
String imagePath = data.getData().getPath();
ImageView imageView = findViewById(R.id.imageView); //WANT TO AVOID THIS AND MAKE IMAGE VIEW NUMBER AS DYNAMIC
Bitmap bmImg = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bmImg);
}
}
}
答案 0 :(得分:0)
InputStream iStream = getContentResolver().openInputStream(selectedImageUri);
byte[] inputData = Utils.getBytes(iStream);
dbHelper.insertImage(inputData);
http://www.coderzheaven.com/2012/12/23/store-image-android-sqlite-retrieve-it/