0
在我的方案中,我從圖庫中選擇一張圖片並將其顯示在同一個佈局的圖像查看中,並且我可以使用圖像處理另一個活動。但是當我點擊意圖按鈕時沒有選擇任何圖像時,我有一個強制停止。如何拋出一個不存在的圖像的異常選擇
誰有解決這個問題的想法?
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView (R.layout.browse_page);
//action for browse Image
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
//declare image and button
image = (ImageView) findViewById (R.id.image);
chooseNewImage = (Button) findViewById (R.id.chooseNewImage);
grayScale = (Button) findViewById (R.id.greyscale);
//back to previous page
chooseNewImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
//go to set the image to grayScale
grayScale.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent greyScale = new Intent(browsePage.this, grayScale.class);
greyScale.putExtra("imagePath", path_selectedImage);
startActivity(greyScale);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == SELECT_PICTURE) {
Uri_selectedImage = data.getData();
path_selectedImage = getPath(Uri_selectedImage);
image.setImageURI(Uri_selectedImage);
System.out.println(path_selectedImage);
Toast.makeText(getBaseContext(), "Your selected picture", Toast.LENGTH_SHORT).show();
}
}
private String getPath(Uri uri) {
// TODO Auto-generated method stub
String[] project = {MediaStore.Images.Media.DATA};
Cursor c = managedQuery(uri, project, null, null, null);
startManagingCursor(c);
int column_index = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
c.moveToFirst();
return c.getString(column_index);
}
每當我沒有選擇圖像,我按下灰度按鈕,它強制停止。是否需要嘗試抓住?
當點擊灰度並且沒有圖像被選中時,你想要發生什麼?或者您應該只在選擇圖像時啓用灰度按鈕?請同時發佈崩潰的logcat。 – Simon
我想彈出一個敬酒〜 – user1381427