2013-07-25 24 views
0

嘿我想要加载2个图像在1个活动中就像比较某事 如果我单击button1我可以加载图像并将图像设置为ImageView 1,如果我点击按钮2我可以加载但无法设置为imagview2为什么?我在哪里做错了?Android - 在1个活动中加载2个图像

检查我的代码有错误

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    overridePendingTransition(R.anim.push_left_in, R.anim.hold); 
    setContentView(R.layout.compare); 

    ImageButton imgBtnCpr1 = (ImageButton) findViewById(R.id.imgBtnCpr1); 

    imgBtnCpr1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(
        Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

      startActivityForResult(i, RESULT_LOAD_IMAGE1); 

     } 
    }); 

    ImageButton imgBtnCpr2 = (ImageButton) findViewById(R.id.imgBtnCpr2); 

    imgBtnCpr2.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent t = new Intent(
        Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

      startActivityForResult(t, RESULT_LOAD_IMAGE2); 
     } 
    }); 
} 

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

    if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK 
      && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

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

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

     ImageView imageViewBrw1 = (ImageView) findViewById(R.id.imgViewBrw1); 
     imageViewBrw1.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

     // -----------------------------------------------------------------------------------// 

     if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK 
       && null != data) { 
      Uri selectedImage2 = data.getData(); 
      String[] filePathColumn2 = { MediaStore.Images.Media.DATA }; 

      Cursor cursor2 = getContentResolver().query(selectedImage2, 
        filePathColumn2, null, null, null); 
      cursor2.moveToFirst(); 

      int columnIndex2 = cursor2.getColumnIndex(filePathColumn2[0]); 
      String picturePath2 = cursor2.getString(columnIndex2); 
      cursor2.close(); 

      ImageView imageViewBrw2 = (ImageView) findViewById(R.id.imgViewBrw2); 
      imageViewBrw2.setImageBitmap(BitmapFactory 
        .decodeFile(picturePath2)); 
     } 

    } 
} 

回答

0

更改代码这样

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

    if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK 
      && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

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

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

     ImageView imageViewBrw1 = (ImageView) findViewById(R.id.imgViewBrw1); 
     imageViewBrw1.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

    } 
    // -----------------------------------------------------------------------------------// 

    if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK 
       && null != data) { 
      Uri selectedImage2 = data.getData(); 
      String[] filePathColumn2 = { MediaStore.Images.Media.DATA }; 

      Cursor cursor2 = getContentResolver().query(selectedImage2, 
        filePathColumn2, null, null, null); 
      cursor2.moveToFirst(); 

      int columnIndex2 = cursor2.getColumnIndex(filePathColumn2[0]); 
      String picturePath2 = cursor2.getString(columnIndex2); 
      cursor2.close(); 

      ImageView imageViewBrw2 = (ImageView) findViewById(R.id.imgViewBrw2); 
      imageViewBrw2.setImageBitmap(BitmapFactory 
        .decodeFile(picturePath2)); 
     } 
} 
+0

哎穆赫辛你可以帮我要回答我的问题http://stackoverflow.com/questions/17824406/ Android的如何 - 发送图像到其他活动,从负载图像 - 从-SD卡 –

相关问题