passing gallery image to another activity through intent - android

I need to send a image from gallery to another activity by using the path of that image but nothing happened...
help me out......
here is the code..
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==RESULT_LOADIMAGE&&resultCode==RESULT_OK&&null!=data)
{
Uri selectedImages=data.getData();
String[] filePathColon={MediaStore.Images.Media.DATA};
Cursor cursr=getContentResolver().query(selectedImages, filePathColon, null, null, null);
cursr.moveToFirst();
int columnindex=cursr.getColumnIndex(filePathColon[0]);
String picturepath=cursr.getString(columnindex);
cursr.close();
Intent intent= new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("imagePath",filePathColon );
startActivity(intent);
}
}
and second activity code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
img=(ImageView)findViewById(R.id.imageView1);
getData();
}
private void getData(){
String ps=getIntent().getStringExtra("imagepath");
img.setImageBitmap(BitmapFactory.decodeFile(ps));
}

intent.putExtra("imagePath",filePathColon );
should be
intent.putExtra("imagePath",picturepath);
because filepathcolon refers to column index and picturepath refers to the URI
and get it using
String ps=getIntent().getStringExtra("imagePath"); //it should be same as you send it

please change like this
Intent intent= new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("imagePath",picturepath);
startActivity(intent);
}

The two strings in your Intent do not match:
You wrote imagePath with capital P in the first Activity, but not in your second one.
String ps=getIntent().getStringExtra("imagePath");
fixes it

Related

Draw bitmap in different activity

So my app should capture image with intent then draw that image in different activity, but it doesn't work. Here is my code:
Main activity:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, 2);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Intent k = new Intent(MainActivity.this, FullActivity.class);
startActivity(k);
}
}
Second activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri selectedImage = imageUri;
iv.setImageBitmap(bp);
}
Can someone help me and show where the problem is. Thanks in advance.
You need to pass the Uri as a string with the Intent in your MainActivity.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Intent k = new Intent(MainActivity.this, FullActivity.class);
k.putExtra("uri", imageUri.toString());
startActivity(k);
}
}
Then get the string in your FullActivity and parse it to a Uri, then use that to set your ImageView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
String uriString = getIntent().getStringExtra("uri");
Uri selectedImage = Uri.parse(uriString);
// you may also need to call imageView.setImageURI(null); here
// or get the bitmap first to use imageView.setImageBitmap();
imageView.setImageURI(selectedImage);
}
From here
Called when an activity you launched exits, giving you the requestCode
you started it with, the resultCode it returned, and any additional
data from it
Your onActivityResult never get called when starting Second activity
Try send the uri you receive in Main activity in Bundle
Uri imageUri = intent.getData();
Intent intent = new Intent(MainActivity.this, SecondActiviy.class);
intent.putExtra("mUri", imageUri.toString());
And you can get this uri on SecondActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String uri= getIntent().getStringExtra("mUri");
Uri imageUri= Uri.parse(uri);
Bitmap bitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
}
// Global Declaration<br>
private Uri fileUri;
private static final int REQUEST_CODE_PHOTO = 101;
// Click for capture Image
fileUri = getOutputMediaFileUri(REQUEST_CODE_PHOTO); // create a file to save the image
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, REQUEST_CODE_PHOTO);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PHOTO) {
if (resultCode == RESULT_OK) {
String PATH = fileUri.getPath();
Intent k = new Intent(MainActivity.this, FullActivity.class);
k.putExtra("KEY_FOR_PATH, PATH);
startActivity(k);
}
}
}
}
// In FullActivity
String pathOfFile = getIntent().getExtras().getString("KEY_FOR_PATH");
Bitmap myBitmap = BitmapFactory.decodeFile(pathOfFile);
//Now set myBitmap in ImageView
imageView.setImageBitmap(myBitmap)
// Add this method
private Uri getOutputMediaFileUri(int type){
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "YOUR IMAGE FOLDER");
if(imagesFolder != null && !imagesFolder.exists()) {
imagesFolder.mkdirs();
}
int photonum = AppTypeDetails.getInstance(MainActivity.this).getImageName();
AppTypeDetails.getInstance(ReportTab.this).setImageName(++photonum);
File image = new File(imagesFolder, String.format("image%08d.jpeg", photonum));
return Uri.fromFile(image);
}

How to use Intent in onResume?

I'm trying to send an image path from one activity to another. I'm catching the intent in onResume, but the string path is always null. I don't know what I'm doing wrong. Hopefully you guys can help me with this problem.
Here's my activity where I grab the image path and send an intent.
private Intent testImage = new Intent(this, MyActivity.class);
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.card_create_layout);
testImage = new Intent(this, MyActivity.class);
}
private void grabImage()
{
Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(imageGetter, RESULT_LOAD_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
user_image_path = cursor.getString(columnIndex);//here we have our image path.
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(user_image_path));
}
testImage.putExtra("the_image", user_image_path);
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.theCreateButton:
grabImage();
break;
case R.id.theDesButton:
startActivity(sendInformation);
}
}
#Override
public void onBackPressed()
{
super.onBackPressed();
MyActivity.checkCard();
setResult(Activity.RESULT_OK, getIntent());
finish();
}
Now in my other activity, when I grab the image and press back
#Override
public void onResume()
{
super.onResume();
String ImagePath = getIntent().getStringExtra("the_image");
if(ImagePath == null)
{
Toast.makeText(this,"hello everyone",Toast.LENGTH_LONG).show();
}
}
It keeps on showing the toast message "hello everyone", which means ImagePath is continuously null. How do I fix this?
Pass mechanism between activities is available in three ways :
via DI(Dependency Injection)
via Bundle mechanism
via Singletone class which play a role a bridge or data holder between activities.
To avoid duplicating answer - please search any way(i recommend easiest - via Bundle) in stackoverflow.
Quick guide :
You put your string into bundle via intent.putExtra(String name, String value) in Activity A
Start this intent with startActivity(intent);
In B activity read value view getIntent().getStringExtra(String name) in OnCreate method.
name value is need the same in activity A and B. This is a key.
I don't see a
startActivity(testImage);
If you aren't using that intent to start the activity, then there is no extra called 'the_image' and the getStringExtra function will effectively return a null.
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("the_image", user_image_path);
setResult(RESULT_OK, intent);
super.onBackPressed();
}
The above is how to correctly do it, if you have started an activity for result.

Sending data from one activity to another startactivityforresult

I searched on the forums but couldn't find the right answer for me.
I've included the relevant parts below
ACTIVITY ONE
implicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startImplicitActivation() when pressed
#Override
public void onClick(View v) {
Intent myIntent = new Intent(ActivityLoaderActivity.this,
ExplicitlyLoadedActivity.class);
startActivityForResult(myIntent, GET_TEXT_REQUEST_CODE);
}
});
and a little lower
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
String input=data.getStringExtra(TAG);
mUserTextView.setText(input);
}
This is activity 2 after user enters some data
String input=mEditText.getText().toString();
Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
i.putExtra("TAG",input);
startActivity(i);
this.setResult(RESULT_OK);
finish();
No error messages at all but the text on screen doesnt update. it is supposed to
don't need start activity in second class:
you need change your code with:
Intent i = new Intent(); // or // Intent i = getIntent()
i.putExtra("TAG",input);
setResult(RESULT_OK , i);
finish();
and for cancel that,
setResult(RESULT_CANCELED, i);
finish();
On your Activity2 you are launching a new instance of ExplicityLoadedActivity instead of returning into the previous instance.
You should only set the result, and finish your second activity.
Here's the code you can try on your 2nd activity:
Intent returnIntent = new Intent();
returnIntent.putExtra("TAG",input);
setResult(RESULT_OK, returnIntent);
finish();
try like this
String input=data.getStringExtra("TAG");
in place of
String input=data.getStringExtra(TAG);
Try like this to set result code in ExplicitlyLoadedActivity
String input=mEditText.getText().toString();
Intent i = new Intent();
i.putExtra("TAG",input);
this.setResult(RESULT_OK,i);
finish();
and in ActivityLoaderActivity access that result string i.e.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
String input=data.getStringExtra("TAG");
mUserTextView.setText(input);
}

Getting ArrayList<Spanned> from OnActivityResult function saved inside Intent

I am currently working on an android project and I have an activity that is started using the startActivityForResult() function.
Within this activity I have an ArrayList and I create an Internet and then set the result as the intent, as in the following code.
private void getSearchData()
{
ArrayList<Spanned> passwords = null;
String searchTerm = txtSearch.getText().toString();
GetSearchResults search = new GetSearchResults(this, searchTerm);
if (rdoApp.isChecked())
{
passwords = search.getData(SearchType.App);
}
else if (rdoName.isChecked())
{
passwords = search.getData(SearchType.Name);
}
else if (rdoUsername.isChecked())
{
passwords = search.getData(SearchType.Username);
}
Intent intent = new Intent();
intent.putExtra("searchResults", passwords);
setResult(1, intent);
finish();
}
In the first activity in the function OnActivityResult I then want to get the ArrayList so that I can process the data. I have the following code so far.
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
common.showToastMessage("Result received", Toast.LENGTH_LONG);
Bundle bundle = data.getExtras();
}
I have no idea where to go from here.
I've managed to find a way, it is thank to #Jan Gerlinger answer pointed me in the correct direction but I've found how to do it.
In the activity where I am setting the result I have the following code
ArrayList<Spanned> passwords = search.getResult();
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("passwords", passwords);
intent.putExtras(bundle);
setResult(1, intent);
finish();
In the activity for inside the OnActivityResult function I have the following
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
common.showToastMessage("Result received", Toast.LENGTH_LONG);
Bundle bundle = data.getExtras();
ArrayList<Spanned> passwords = (ArrayList<Spanned>) bundle.getSerializable("passwords");
}
intent.putExtra("searchResults", passwords);
here uses the putExtra(String name, Serializable value) method. So you can use getSerializableExtra(String name) to get it back:
ArrayList<Spanned> passwords = (ArrayList<Spanned>) data.getSerializableExtra("searchResults");
Depending on the type of your Spanned objects and if they do implement Serializable, this may however throw Exceptions as Spanned does not implement Serializable directly.

Pass data from one activity to another [duplicate]

This question already has answers here:
How to pass a value from one Activity to another in Android? [duplicate]
(7 answers)
Closed 9 years ago.
In Activity1, I input some data like name and address. When I click the next button, there will be another input form. What I want to do is, when I click BACK, I will return to Activity1 and the data I entered there previously is shown.
HELP please :)
=============
UPDATED: Activity1
private void startActivityForResult()
{
TextView textname = (TextView) findViewById(R.id.username);
TextView textaddress = (TextView) findViewById(R.id.useraddress);
Intent intent = new Intent(this, GetInformation.class);
//intent.putExtras(getIntent());
intent.putExtra("username", textname.getText().toString());
intent.putExtra("useradd", textaddress.getText().toString());
startActivityForResult(intent, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
TextView textname = (TextView) findViewById(R.id.username);
TextView textaddress = (TextView) findViewById(R.id.useraddress);
textname.setText(data.getStringExtra("returnname").toString());
textaddress.setText(data.getStringExtra("returnadd").toString());
}
Activity2
private void startActivityForResult()
{
final String username;
final String useraddress;
Intent intent = getIntent();
//intent.putExtras(getIntent());
username = getIntent().getStringExtra("username");
useraddress = getIntent().getStringExtra("useradd");
intent.putExtra("returnname", username);
intent.putExtra("returnadd", useraddress);
setResult(0, intent);
}
There's a simple way to do this in Android : startActivityForResult. Basically, when you launch the activity, you say that you are expecting a result. The other activity can then add information that will be returned to the starting activity. Here's a very simple code sample from the official doc :
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}
You can get a much more complete description of all this on the Activity page in the official doc (section Starting Activities and Getting Results).
Save Activity1 state in method onSaveInstanceState, and then in method
void onCreate(Bundle savedInstanceState)
you can restore state, using savedInstanceState.
Or, if you want pass entered data to second activity, you can place data in intent.
Sample:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("Key", "Value");
startActivityForResult(i, 0);
in Second Activity you can get data:
getIntent().getStringExtra("Key");
To return result from second activity:
Intent data = new Intent();
data.put("key", "value");
setResult(RESULT_OK, data);
then you can retrieve data in first activity using
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
data.getStringExtra("key");
}

Categories

Resources