Reputation: 5951
I am trying to pass the image I pick from gallery to another activity. Beside this, in same class, i have several editText
to get data from user. To take data from editText
and pass on SecondActivity
i use 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);
}
});
And in order i have 'SwitchCase' 'onActivityResult' where i use for picking image and set on imageView
on FirstActivity
. This is openGallery
intent for double onActivityResult
:
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);
}
SwitchCase onActivityResult:
@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;
}}
}
}
So now main thing is where to insert below code:
final Intent newdata = new Intent(NewCard.this, Template.class);
newdata.putExtra("picture_path1", PicPath1);
newdata.putExtra("picture_path2", PicPath2);
startActivity(newdata);
Normally, i have to insert in body of save.setOnClickListener
and get data+images to pass SecondActivity
as i do for editText
. But i have clash for Second Intent.
Or I can insert as:
logoview.setBackgroundResource(0);
logoview.setImageBitmap(BitmapFactory.decodeFile(PicPath1));
Intent newdata = new Intent(NewCard.this, Template.class);
newdata.putExtra("picture_path1", PicPath1);
startActivity(newdata);
Then it works but intent starts immediately and i am unable to setText
above. Thank you for your helps.
This is all my code in the class:
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();
}
}
Upvotes: 0
Views: 196
Reputation: 10184
Just store the picpaths as instance variables to your activity. Manage them like any other lifecyled instance variable (via onSaveInstanceState()
, and onCreate(Bundle)
) and have your onClickListener reference them there.
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));
}
}
Then in your save button part:
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);
}
});
Upvotes: 1