繁体   English   中英

Togglebutton结合意图在Android中显示图像

[英]Togglebutton in combination with Intent to display Image in Android

我正在使用切换按钮在使用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);
                    }
                }
            });
        }
    }
}

不知道这是否可以解决所有问题,但:

位图imgb->始终为null。

修复:在您的onActivityResult中:

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

包含Android权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM