Android 坚持使用意图
我正在尝试将我从图库中选择的图像传递到另一个活动。除此之外,在同一个类中,我有几个Android 坚持使用意图,android,android-intent,bitmap,android-activity,Android,Android Intent,Bitmap,Android Activity,我正在尝试将我从图库中选择的图像传递到另一个活动。除此之外,在同一个类中,我有几个editText从用户那里获取数据。要从editText获取数据并传递SecondActivity我使用Intent: save=(Button)findViewById(R.id.save); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { I
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);
}
});
然后在ActivityResult上设置“SwitchCase”,用于拾取图像,并在FirstActivity
上设置imageView
。这是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);
然后它工作了,但意图立即启动,我无法在上面设置文本。谢谢你的帮助
这是我在课堂上的全部代码:
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();
}
}
只需将picpath作为实例变量存储到活动中。像任何其他生命周期实例变量一样管理它们(通过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);
}
});
你能把你现在写的代码发出去吗。。所以我可以帮你。。我的意思是从意图开始谢谢你的关心。上面已经是我的代码了。我只是不知道该把它放在哪里。如果你检查以上我已经有一个编辑文本的意图。第二个是Intent newdata=newintent(NewCard.this,Template.class);newdata.putExtra(“图片路径1”,PicPath1);startActivity(新数据);你能发布你的完整代码吗。注意,不要用大写字母开头java变量,如intent.putExtra(“name”,name)代码>开关(1)将始终调用案例1,而不是案例2,除非您忘记插入breakHow to thank!你让我开心。我试图解决这个复杂的问题,终于解决了。非常感谢@GreggiaCovelli顺便说一句:当我对mPicPath1和mPicPath2使用字符串类型时,我不能使用位图调整大小,或者换句话说,使用inSampleSize函数。你能建议我一个解决方案,拉图像大小较小,因为我不能导入大尺寸图像和位图大小超过虚拟机预算错误。Thx@greggiacovelli不理解路径是字符串并且能够调整大小的限制?我想说的是,在使用BitmapFactory.Options进行解码时,只需按比例缩小