-3

我有一个活动,我在其中打开相机、语音和视频的意图。我正在使用 onActivityResult。但是当 onActivityResult 被调用时,我观察到一些非常奇怪的事情,switch case 中的所有情况都被调用意味着它在所有情况下都是 gng 。

怎么可能。我有什么问题吗?.

下面是我的代码

   protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     {
      super.onActivityResult(requestCode, resultCode, data); 

       if(resultCode == RESULT_OK){




          switch(requestCode) { 

             case 1:

                Uri selectedImage = data.getData();
                System.out.println("selectedimage"+selectedImage);
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage,       filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


                yourSelectedImage = BitmapFactory.decodeFile(filePath);
                /* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in
                 *  way you want! */



                btn_img.setImageBitmap(yourSelectedImage);





        case 0:
            try {
                yourSelectedImage = (Bitmap) data.getExtras().get("data");
                btn_img.setImageBitmap(yourSelectedImage);
                System.out.println("try for 0"+yourSelectedImage);
            } catch (Exception e) {
                // TODO: handle exception
            }

        case 2:
            try {


                    System.out.println("ooookkkkkk");


                        System.out.println("SELECT_AUDIO");
                        Uri selectedImageUri = data.getData();
                        selectedPath = getPath(selectedImageUri);
                        try {
                            System.out.println("try for 2"+yourSelectedImage);
                             btn_img.setImageBitmap(yourSelectedImage);
                        } catch (Exception e) {
                            System.out.println("catch"+yourSelectedImage);
                            // TODO: handle exception
                        }
                        System.out.println("SELECT_AUDIO Path : " + selectedPath);
                       // doFileUpload();





            } catch (Exception e) {
                // TODO: handle exception
            }

        case 3:
            try {




                        System.out.println("SELECT_AUDIO");
                        Uri selectedImageUri = data.getData();
                        selectedPath = getPath(selectedImageUri);
                        try {
                            System.out.println("try for 3"+yourSelectedImage);
                             btn_img.setImageBitmap(yourSelectedImage);
                        } catch (Exception e) {
                            System.out.println("catch"+yourSelectedImage);
                            // TODO: handle exception
                        }
                        System.out.println("SELECT_AUDIO Path : " + selectedPath);
                       // doFileUpload();




            } catch (Exception e) {
                // TODO: handle exception
            }

        }


    }






}
4

2 回答 2

12

break;在每个案例的末尾添加并检查

switch(requestCode) { 
   case 1:
    //code
    break;
   case 2:
    //code
   break;
   .
   .
   default:

   break;
}
于 2012-10-03T10:11:41.340 回答
4

在每个案例之后使用“break”语句。

前任:

  switch(value)
    {
    case 1:
    //code..
    break;

    case 2:
    //code
    break;
    }
于 2012-10-03T10:13:18.333 回答