当我使用下面的代码时,此代码适用于android kitkat版本,但它不适用于棒棒糖和marshmallow版本。当我选择一个图像时,它不会在imageview中显示并在棒棒糖和棉花糖中抛出异常。
在这里,我附上了代码,用于在按钮点击时显示图库中的图像。
FloatingActionButton pick = (FloatingActionButton) findViewById(R.id.pick);
pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(imgDecodableString,options);
ImageView imgView = (ImageView) findViewById(R.id.imageView);
int height = bitmap.getHeight() * 4, width = bitmap.getWidth() * 4;
Toast.makeText(this, "Height" + height + "Width" + width, Toast.LENGTH_LONG).show();
if (height > 4096 || width > 4096){
bitmap1 = BitmapFactory.decodeFile(imgDecodableString,options);
imgView.setImageBitmap(bitmap1);
Toast.makeText(this, "Need to Resize", Toast.LENGTH_LONG).show();
}else {
bitmap1 = BitmapFactory.decodeFile(imgDecodableString);
imgView.setImageBitmap(bitmap1);
Toast.makeText(this, "Works", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}