Frage

I'm trying to count that how many colors there are in the selected image from gallery, but i couldn't fix the problem in my code, i'm new at android , please help me .

public class MainActivity extends 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());

        }


    }
 }
War es hilfreich?

Lösung

I think that the problem could be that you are trying to use setText with an argument that is not a String.

Try this code:

txt.setText(String.valueOf(colors.size()));

I hope this helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top