gpt4 book ai didi

java - Android中Togglebutton结合Intent显示Image

转载 作者:行者123 更新时间:2023-12-01 10:52:09 25 4
gpt4 key购买 nike

我正在使用切换按钮在使用 Intent 获取的图像(从 SD 卡中选择)之间切换。但是,我选择图像后出现错误。基本上我的想法是浏览图像并根据切换按钮的状态显示在(原始图像)Imageview 上。此外,当我更改切换按钮的状态时,我应该看到不同的图像。

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

最佳答案

不确定这是否可以解决所有问题,但是:

位图 imgb -> 将始终为空。

修复:在您的 onActivityResult 中:

try {
streamI = getContentResolver().openInputStream(imagef);
//Create bitmap from selected image
imgb = BitmapFactory.decodeStream(streamI);
.....
}

关于java - Android中Togglebutton结合Intent显示Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com