卡在意图的使用上



我正在尝试将我从图库中选择的图像传递给另一个活动。除此之外,在同一类中,我有几个editText可以从用户那里获取数据。要从editText获取数据并传递SecondActivity我使用 Intent

save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
    @Override   
    public void onClick(View v) {
    Intent intent = new Intent(NewCard.this, Template.class);
            intent.putExtra("name", Name);
            intent.putExtra("company", Company);
            intent.putExtra("phone", Phone);
            intent.putExtra("mobile", Mobile);
            intent.putExtra("address", Address);
            intent.putExtra("email", Email);
            intent.putExtra("website", Website);
            intent.putExtra("title", Title); 
            startActivity(intent);
        }
    });

为了我有"开关案例"onActivityResult",我用它来挑选图像并在FirstActivity上设置imageView。这是双重onActivityResult openGallery意图:

private void openGallerylogo() {
    // TODO Auto-generated method stub
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
}
private void openGalleryqr() {
    // TODO Auto-generated method stub
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 2);
}

开关案例打开活动结果:

@Override
protected void onActivityResult(int requestCode, int resultcode, Intent data){
    super.onActivityResult(requestCode, resultcode, data);
    switch(1){
    case 1:
    if (requestCode == 1) 
    {
        if (data != null && resultcode == RESULT_OK) 
        {              
            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 PicPath1 = cursor.getString(columnIndex);
            cursor.close();
    logoview.setBackgroundResource(0);
    logoview.setImageBitmap(BitmapFactory.decodeFile(PicPath1));
    break;
        }}  

    case 2:
        if (requestCode == 2) 
        {
            if (data != null && resultcode == RESULT_OK) 
            {              
                Uri selectedImage = data.getData();                     
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                cursor.moveToFirst();                   
                String PicPath2 = cursor.getString(columnIndex);
                cursor.close();
    qrcodeview.setBackgroundResource(0);
    qrcodeview.setImageBitmap(BitmapFactory.decodeFile(PicPath2));
    break;
}}
    }
    }

所以现在主要是在哪里插入下面的代码:

    final Intent newdata = new Intent(NewCard.this, Template.class);
    newdata.putExtra("picture_path1", PicPath1);
    newdata.putExtra("picture_path2", PicPath2);
    startActivity(newdata);

通常,我必须插入save.setOnClickListener体并获取数据+图像以传递SecondActivity,就像我为editText所做的那样。但我有第二意图的冲突。

或者我可以插入为:

logoview.setBackgroundResource(0);
logoview.setImageBitmap(BitmapFactory.decodeFile(PicPath1));
Intent newdata = new Intent(NewCard.this, Template.class);
newdata.putExtra("picture_path1", PicPath1);
startActivity(newdata);

然后它有效,但意图立即开始,我无法setText上面。谢谢你的帮助。


这是我在课堂上的所有代码:

package com.example.mapcard;
public class NewCard extends Activity {
ImageView logoview,qrcodeview, location;
Bitmap bmp1,bmp2;
Button save,clear;
public String count="logo";
EditText txtName,txtCompany,txtPhone,txtMobile,txtAddress,txtEmail,txtWebsite,txtTitle;

//  public String url = Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.example.mapcard/Mapcard/BusinessCard/";
public String filename = "Mapcard.csv";
//  public String fileContent;
ReadCSV csv;
StringBuffer filePath;
//String picPath1;
File file;
public final static String EXTRA_MESSAGE = "com.example.mapcard.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_card);

    txtName= (EditText) findViewById(R.id.txtName);
    txtCompany= (EditText) findViewById(R.id.txtCompany);
    txtPhone= (EditText) findViewById(R.id.txtPhone);
    txtMobile = (EditText) findViewById(R.id.txtMobile);
    txtAddress = (EditText) findViewById(R.id.txtAddress);
    txtEmail = (EditText) findViewById(R.id.txtEmail);
    txtWebsite = (EditText) findViewById(R.id.txtWebsite);
    txtTitle = (EditText) findViewById(R.id.txtTitle);

filePath = new StringBuffer();
   filePath.append("/sdcard/Android/data/");
    //filePath.append(Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.example.mapcard/Mapcard/BusinessCard/");
    file = new File(filePath+filename.toString());
    csv = new ReadCSV(file);
    save=(Button)findViewById(R.id.save);
    save.setOnClickListener(new View.OnClickListener() {
        @Override   
        public void onClick(View v) {
            // TODO Auto-generated method stub
//              GenerateCsv.generateCsvFile1(url, filename, fileContent);
            csv.writeHeader(txtName.getText().toString()+ "," 
                    +txtCompany.getText().toString()
                    + "," +txtPhone.getText().toString()
                    + "," +txtMobile.getText().toString()
                    + "," +txtAddress.getText().toString()
                    + "," +txtEmail.getText().toString()
                    + "," +txtWebsite.getText().toString()
                    + "," +txtTitle.getText().toString());

            Toast.makeText(getApplicationContext(), "Saved Directory!", Toast.LENGTH_SHORT).show();
   //    startActivity(new Intent(NewCard.this, Main.class));

            EditText txtName = (EditText) findViewById(R.id.txtName);
            EditText txtCompany= (EditText) findViewById(R.id.txtCompany);
            EditText txtPhone= (EditText) findViewById(R.id.txtPhone);
            EditText txtMobile = (EditText) findViewById(R.id.txtMobile);
            EditText txtAddress = (EditText) findViewById(R.id.txtAddress);
            EditText txtEmail = (EditText) findViewById(R.id.txtEmail);
            EditText txtWebsite = (EditText) findViewById(R.id.txtWebsite);
            EditText txtTitle = (EditText) findViewById(R.id.txtTitle);

            String Name=txtName.getEditableText().toString(),
            Company=txtCompany.getEditableText().toString(), Phone=txtPhone.getEditableText().toString(), 
            Mobile=txtMobile.getEditableText().toString(), Address=txtAddress.getEditableText().toString(), 
            Email=txtEmail.getEditableText().toString(), Website=txtWebsite.getEditableText().toString(), 
            Title=txtTitle.getEditableText().toString();                
            Intent intent = new Intent(NewCard.this, Template.class);
            intent.putExtra("name", Name);
            intent.putExtra("company", Company);
            intent.putExtra("phone", Phone);
            intent.putExtra("mobile", Mobile);
            intent.putExtra("address", Address);
            intent.putExtra("email", Email);
            intent.putExtra("website", Website);
            intent.putExtra("title", Title); 
              startActivity(intent);
        }
    });
    clear=(Button)findViewById(R.id.clear);
    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Screen Cleared!", Toast.LENGTH_SHORT).show();
            txtName.getText().clear();
            txtCompany.getText().clear();
            txtPhone.getText().clear();
            txtMobile.getText().clear();
            txtAddress.getText().clear();
            txtEmail.getText().clear();
            txtWebsite.getText().clear();               
            txtTitle.getText().clear();

        }
    });

    location=(ImageView)findViewById(R.id.location);
    location.setOnClickListener(new OnClickListener()
    {
    @Override
    public void onClick(View v) {
    //  startActivity(new Intent(NewCard.this, GoogleMap.class));
        EditText txtAddress = (EditText) findViewById(R.id.txtAddress);
        String Address=txtAddress.getEditableText().toString();
        Intent intent = new Intent(NewCard.this, GoogleMap.class);
        intent.putExtra("address", Address);
        startActivity(intent);  
    }
    });
    logoview=(ImageView)findViewById(R.id.logoview);
    logoview.setOnClickListener(new OnClickListener() 
    {            
        @Override
        public void onClick(View arg0) {
            //startActivity(new Intent(NewCard.this, Logo.class));
            count="logo";
       //               openGallery1();
            openGallerylogo();
        }

    }); 
    qrcodeview=(ImageView)findViewById(R.id.qrcodeview);
    qrcodeview.setOnClickListener(new OnClickListener() 
    {            

        @Override
        public void onClick(View arg0) {
            count="qr";
  //                openGallery();
            openGalleryqr();
        }
    }); 

}
private void openGallerylogo() {
    // TODO Auto-generated method stub
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
}
private void openGalleryqr() {
    // TODO Auto-generated method stub
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 2);
}

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

    switch(1){
    case 1:
    if (requestCode == 1) 
    {
        if (data != null && resultcode == RESULT_OK) 
        {              
            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 PicPath1 = cursor.getString(columnIndex);
            cursor.close();
                // intent.getExtras().get("data");
        //    startActivity(newdata);
                 //             if(bmp1 != null && !bmp1.isRecycled())

//              ImageView logoview = (ImageView) findViewById(R.id.logoview);
//              logoview.setImageBitmap(BitmapFactory.decodeFile(filePath));
        //  bmp1 = BitmapFactory.decodeFile(PicPath1);
 //             if (count.equals("logo")){
                logoview.setBackgroundResource(0);
                logoview.setImageBitmap(BitmapFactory.decodeFile(PicPath1));
                Intent newdata = new Intent(NewCard.this, Template.class);
                newdata.putExtra("picture_path1", PicPath1);
 //newdata.putExtra("picture_path2", PicPath2);
                startActivity(newdata);

                 //   newdata.putExtra("picture_path2", PicPath2);
            break;
        }}  

    case 2:
        if (requestCode == 2) 
        {
            if (data != null && resultcode == RESULT_OK) 
            {              
                Uri selectedImage = data.getData();                     
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                cursor.moveToFirst();                   
                String PicPath2 = cursor.getString(columnIndex);
                cursor.close();


                    qrcodeview.setBackgroundResource(0);
            qrcodeview.setImageBitmap(BitmapFactory.decodeFile(PicPath2));              
        break;
        }}
    }
    }

 //     final Intent newdata = new Intent(NewCard.this, Template.class);
 //     newdata.putExtra("picture_path1", PicPath1);
 //     newdata.putExtra("picture_path2", PicPath2);
//      startActivity(newdata);
//      
//  }
@Override
protected void onPause(){
    super.onPause();
}
}

只需将图片路径存储为活动的实例变量即可。像管理任何其他生命周期实例变量一样管理它们(通过 onSaveInstanceState()onCreate(Bundle) ),并让您的 onClickListener 在那里引用它们。

String mPicPath1, mPicPath2;
protected void onCreate(Bundle icicle) {
   mPicPath1 = null;
   mPicPath2 = null; // or restore these from the icicle Bundle
   ...
}
@Override
protected void onActivityResult(int requestCode, int resultcode, Intent data){
    super.onActivityResult(requestCode, resultcode, data);
    switch(requestCode){
    case 1:
        if (data != null && resultcode == RESULT_OK) 
        {              
            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]);
            mPicPath1 = cursor.getString(columnIndex);
            cursor.close();
            logoview.setBackgroundResource(0);
            logoview.setImageBitmap(BitmapFactory.decodeFile(mPicPath1));
        }
    break;  
    case 2:
        if (data != null && resultcode == RESULT_OK) 
        {              
            Uri selectedImage = data.getData();                     
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            cursor.moveToFirst();                   
            mPicPath2 = cursor.getString(columnIndex);
            cursor.close();
            qrcodeview.setBackgroundResource(0);
            qrcodeview.setImageBitmap(BitmapFactory.decodeFile(mPicPath2));
        }
    break;
    }
    View saveButton = findViewById(R.id.save);
    // If the image paths are required before the activity can be started you can do this
    if (saveButton != null) {
       saveButton.setEnabled( !TextUtils.isEmpty(mPicPath1) &&  !TextUtils.isEmpty(mPicPath2));
    }
}

然后在您的保存按钮部分:

save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override   
public void onClick(View v) {
Intent intent = new Intent(NewCard.this, Template.class);
        intent.putExtra("name", Name);
        intent.putExtra("company", Company);
        intent.putExtra("phone", Phone);
        intent.putExtra("mobile", Mobile);
        intent.putExtra("address", Address);
        intent.putExtra("email", Email);
        intent.putExtra("website", Website);
        intent.putExtra("title", Title); 
        // Add your picture paths if they are appropriate
        if (!TextUtils.isEmpty(mPicPath1)) {
            intent.putExtra("picture_path1", PicPath1);
        }
        if (!TextUtils.isEmpty(mPicPath2)) {
           intent.putExtra("picture_path2", PicPath2);
        }
        startActivity(intent);
    }
});

相关内容

最新更新