-1

我正在尝试下面的代码将图像从一个活动传递到另一个活动。

        Bundle search_opt=new Bundle();
         search_opt.putByteArray("key1", image1);
        search_opt.putByteArray("key2", image2);
        search_opt.putByteArray("key3", image3);

        Intent view=new Intent(CameraOpen.this,insertion_db.class);
        view.putExtras(search_opt);
        startActivity(view);

但我得到错误

        Bundle search_opt=new Bundle();
        error :Unreachable code
4

5 回答 5

0

感谢您发布代码。

我将您之前使用的代码放入一个新项目中,并且(在添加了一些虚拟变量和类之后)它编译得很好,不会出现任何无法访问的代码错误。

所以我只能推测可能是什么问题。如果我将 case R.id.okImage 语句向下移动几行,这会使程序执行的正常流程无法到达 Bundle 行并给出无法访问的代码错误消息。我也可以通过注释掉 R.id.okImage 行来获得类似的信息。

// From the switch statement, program execution continues at either
// 1a) the case statement R.id.ibTakePic
// 1b) the case statement R.id.okImage
// 2a) a default statement (if you have entered one)
// 2b) or otherwise at the line following the switch
switch (v.getId()) {
    case R.id.ibTakePic:  // 1a
        for(int x=0;x<3;x++)
        {
            i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i,cameraData);
        }
        break;

        // Thus the program will never run this code as it is after the break from the
        // previous statement and before the case line below.
        // This is an example of unreachable code
        Bundle search_opt=new Bundle();
        search_opt.putByteArray("key1", image1);
        search_opt.putByteArray("key2", image2);
        search_opt.putByteArray("key3", image3);

    case R.id.okImage:  // 1b
        Intent view=new Intent();
        view.putExtras(search_opt);
        startActivity(view);
        break;
    }
    // 2b
}
于 2012-12-13T23:58:13.613 回答
0

我以前试过这个

@Override
public void onClick(View v) {

  switch (v.getId()){
    case R.id.ibTakePic:
     for(int x=0;x<3;x++)
       {
        i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i,cameraData);
       }
        break;
    case R.id.okImage:
        Bundle search_opt=new Bundle();
       search_opt.putByteArray("key1", image1);
       search_opt.putByteArray("key2", image2);
       search_opt.putByteArray("key3", image3);

       Intent view=new Intent(CameraOpen.this,insertion_db.class);
       view.putExtras(search_opt);
       startActivity(view);

        break;
    }
}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  super.onActivityResult(requestCode, resultCode, data);

  if(resultCode==RESULT_OK && counter==1 ){       
                    Bundle extras=data.getExtras();
                    bmp1=(Bitmap) extras.get("data");
      iv1.setImageBitmap(bmp1);
                    byte[] image1 =Utilities.getBytes(bmp1);
                    counter++;  
             }

  else if(resultCode==RESULT_OK && counter==2){
                   Bundle extras=data.getExtras();
                   bmp2=(Bitmap) extras.get("data");
         iv2.setImageBitmap(bmp2);
                   byte[] image2 =Utilities.getBytes(bmp2);
                   counter++;
  }
            else if(resultCode==RESULT_OK && counter==3){
                  Bundle extras=data.getExtras();
                  bmp3=(Bitmap) extras.get("data");
        iv3.setImageBitmap(bmp3);
                  byte[] image3 = Utilities.getBytes(bmp3);
              } } }
于 2012-12-14T20:36:11.037 回答
0

现在通过这样做它可以工作

    case R.id.okImage:
        startother();
        break;
    }
}


private void startother() {         

    Intent view=new Intent(CameraOpen.this,insertion_db.class);
    view.putExtras(search_opt);
    startActivity(view);    
   }


@Override   

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

    if(resultCode==RESULT_OK && counter==1 ){
                    Bundle extras=data.getExtras();
                    bmp1=(Bitmap) extras.get("data");
        iv1.setImageBitmap(bmp1);
                    byte[] image1 =Utilities.getBytes(bmp1);                       
                    search_opt.putByteArray("key1", image1);
        counter++; 
             }

    else if(resultCode==RESULT_OK && counter==2){
                    Bundle extras=data.getExtras();
                    bmp2=(Bitmap) extras.get("data");
        iv2.setImageBitmap(bmp2);
                    byte[] image2 =Utilities.getBytes(bmp2);               
                    search_opt.putByteArray("key2", image2);
        counter++;
    }       
           else if(resultCode==RESULT_OK && counter==3){  
                Bundle extras=data.getExtras(); 
                bmp3=(Bitmap) extras.get("data");
            iv3.setImageBitmap(bmp3);   
                byte[] image3 =Utilities.getBytes(bmp3);
        search_opt.putByteArray("key3", image3); }

}
}

于 2012-12-14T20:49:30.163 回答
0

您可以将图像保存到磁盘并将路径/文件名作为字符串传递给其他活动,而不是传递图像。

于 2012-12-14T20:54:38.900 回答
0

将图像从一个意图传递到另一个意图的简单方法。首先activity.java

intent.putExtra("resId", R.drawable.image);                                 
startActivity(intent);                                                         
Bundle bundle = getIntent().getExtras();                                       
if (bundle != null) {                                                          
    int resId = bundle.getInt("resId");                                        
    imageView.setImageResource(resId);                                         
}
于 2020-09-19T11:41:18.833 回答