0

我正在使用切换按钮在使用 Intent 获得的图像之间切换(从 sdcard 中选择)。但是,我选择图像后出现错误。基本上我的想法是浏览图像并根据切换按钮的状态在(原始图像)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);
                    }
                }
            });
        }
    }
}
4

2 回答 2

1

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

位图 imgb -> 将始终为空。

修复:在您的 onActivityResult 中:

try {
    streamI = getContentResolver().openInputStream(imagef);
    //Create bitmap from selected image
    imgb = BitmapFactory.decodeStream(streamI);
    .....
}
于 2015-11-19T12:36:42.823 回答
0

包括安卓权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
于 2015-11-19T17:51:04.273 回答