2015-11-19 58 views
0

我正在使用togglebutton在使用Intent(從sdcard中選擇)獲取的圖像之間切換。不過,我選擇圖片後出現錯誤。基本上我的想法是瀏覽圖像並根據togglebutton的狀態在(originalimage)Imageview上顯示。而且,當我改變togglebutton的狀態時,我應該看到不同的圖像。Togglebutton與意圖結合在Android中顯示圖像

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

回答

0

包括Android的許可。

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

不知道這一切修復,但是:

位圖IMGB - >將永遠是零。

修復: 在你的onActivityResult:

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

我忘了在manifest.xml文件中添加權限。代碼工作正常。然而只有問題仍然存在,直到我按下切換按鈕,圖像纔會顯示。 –

+0

sulabh - 嘗試在setoncheckedlistener之前放置togglebutton.setchecked(true) – JungJoo

+0

這是因爲OnCheckedChangeListener只在您按下(更改)按鈕時被調用;) – TheTool