我正在尝试计算所选图片中有多少种颜色来自图库,但我无法修复我的代码中的问题,我是android新手,请帮帮我 的
公共类MainActivity扩展了Activity {
private static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && 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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
String asd = picturePath;
ImageView imageView = (ImageView) findViewById(R.id.imgView);
Bitmap originalBm = BitmapFactory.decodeFile(picturePath);
Bitmap bmSolüst = Bitmap.createBitmap(originalBm, 0, 0, originalBm.getWidth()/2, (originalBm.getHeight() / 2));
ImageView imageView2 = (ImageView) findViewById(R.id.imgView2);
imageView2.setImageBitmap(bmSolüst);
Set<Integer> colors = new HashSet<Integer>();
int w = bmSagalt.getWidth();
int h = bmSagalt.getHeight();
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int pixel = bmSolüst.getPixel(x, y);
colors.add(pixel);
}
TextView txt = (TextView) findViewById(R.id.textView2);
txt.setText(colors.size());
}
}
}
答案 0 :(得分:0)
我认为问题可能在于您尝试将setText与不是String的参数一起使用。
试试这段代码:
txt.setText(String.valueOf(colors.size()));
我希望这会有所帮助。