我正在使用togglebutton在使用Intent(从sdcard中选择)获得的图像之间切换。但是我选择图像后出错了。基本上我的想法是浏览图像并在(原始图像)Imageview上显示基于togglebutton的状态。此外,当我改变togglebutton的状态时,我应该看到不同的图像。
public class LoadImage extends AppCompatActivity {
public static final int REQUEST_CODE = 10;
ToggleButton togglebtn;
Bitmap imgb;
Button browseimagebtn;
Bitmap operation;
ImageView originalimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_load);
originalimage = (ImageView) findViewById(R.id.originalimage);
browseimagebtn = (Button) findViewById(R.id.browseimagebtn);
//browse image button clicked
browseimagebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent rawIntent = new Intent(Intent.ACTION_PICK);
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
String pictureDirectoryPath = pictureDirectory.getPath();
Uri data=Uri.parse(pictureDirectoryPath);
rawIntent.setDataAndType(data, "image/*");
startActivityForResult(rawIntent, REQUEST_CODE);
}
});
//browse image button long pressed
browseimagebtn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Loads image from Picture Gallery", Toast.LENGTH_SHORT).show();
return true;
}
});
//toggle switch to decide which image to be displayed on screen
togglebtn = (ToggleButton) findViewById(R.id.togglebtn);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
if(requestCode==REQUEST_CODE){
final float red = (float) 0.299;
final float green = (float) 0.587;
final float blue = (float) 0.114;
final Uri imagef = data.getData();
InputStream streamI;
try {
streamI = getContentResolver().openInputStream(imagef);
//Create bitmap from selected image
Bitmap imgb = BitmapFactory.decodeStream(streamI);
//Define rows and columns of selected image
int rows = imgb.getHeight();int cols = imgb.getWidth();
operation = Bitmap.createBitmap(cols, rows, imgb.getConfig());
//Convert original image to Gray Image
for (int i=0;i<cols;i++){
for(int j=0;j<rows;j++){
int p = imgb.getPixel(i,j);
int r = Color.red(p);
int g = Color.green(p);
int b = Color.blue(p);
r = (int) (red*r);
g = (int) (green*g);
b = (int) (blue*b);
int gray = (int) (r*0.299+g*0.587+b*0.114);
operation.setPixel(i, j, Color.argb(Color.alpha(p), gray, gray, gray));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();}
}
togglebtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "if is checked", Toast.LENGTH_SHORT).show();
originalimage.setImageBitmap(operation);
} else {
Toast.makeText(getApplicationContext(), "else is checked", Toast.LENGTH_SHORT).show();
originalimage.setImageBitmap(imgb);
}
}
});
}
}
}
答案 0 :(得分:1)
不确定这是否会解决所有问题:
位图imgb - &gt;将永远为空。
修正: 在你的onActivityResult:
try {
streamI = getContentResolver().openInputStream(imagef);
//Create bitmap from selected image
imgb = BitmapFactory.decodeStream(streamI);
.....
}
答案 1 :(得分:0)
包含Android权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>