如何将图像从一个活动传递到另一个活动?

时间:2012-12-13 23:52:08

标签: android-intent

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

        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

5 个答案:

答案 0 :(得分:0)

感谢发布代码。

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

所以我只能推测可能出现的问题。如果我将案例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
}

答案 1 :(得分:0)

我之前尝试过这个

  
    

@覆盖
        public void onClick(查看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);
              } } }
    
  

答案 2 :(得分: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); }
         

}
    }

  

答案 3 :(得分:0)

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

答案 4 :(得分: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);                                         
}