Intent getStringExtra returning null - android

I know this question has been asked a lot, but I have no idea where I went wrong..
So I sent some data through one activity and retrieving it from the other activity.
FirstActivity
Timber.i("Photo img : %s", posterUrl);
String url = AppConstants.IMAGE_BASE_URL+
AppConstants.POSTER_SIZE_ORIGINAL+ posterUrl;
Intent i = new Intent(this, ImageFullActivity.class);
i.putExtra(AppConstants.IMAGE_URL, url);
startActivity(i);
Recieving Activity
Intent i = new Intent();
String imageUrl = i.getStringExtra(AppConstants.IMAGE_URL);
Timber.i("GOt image url %s", imageUrl);
Picasso.with(this)
.load(imageUrl)
.into(image);
And I checked. I am not passing null through the extras. The posterUrl is a valid string.

You need to use getIntent() to get the intent instance that started the next activity as
getIntent().getStringExtra
instead of creating a new empty one
//Intent i = new Intent(); remove
Intent i = getIntent(); // or do this

Try this
//Intent i = new Intent();
String imageUrl = getIntent().getStringExtra(AppConstants.IMAGE_URL);
Timber.i("GOt image url %s", imageUrl);
Picasso.with(this)
.load(imageUrl)
.into(image);
OR
Bundle bundle = getIntent().getExtras();
String imageUrl = bundle.getString(AppConstants.IMAGE_URL);
Timber.i("GOt image url %s", imageUrl);
Picasso.with(this)
.load(imageUrl)
.into(image);

String imageUrl = getIntent().getStringExtra(AppConstants.IMAGE_URL);
You need to get the intent by getIntent() method

Problem is that you are trying to create new intent in your receiving activity which does not have your data what you passed in other activity.
To get the intent what you passed in other activity use getIntent().
Intent i = getIntent();
Here this intent will be holding your data you passed from other activity.

Related

sending image to another activity

I am trying to send an image that in ListView from onItemClick to onCreate method in another activity .
This is my code :
Activity 1
product_imageView= findViewById(R.id.product_imageView);
byte[] image = savedInstanceState.getByteArray("Product image");
Bitmap receivedImage = BitmapFactory.decodeByteArray(image,0,image.length);
//here it give me error
product_imageView.setImageResource(receivedImage);
Activity 1
String productimage = productslist.get(position).getProductImage();
Bundle bundle = new Bundle();
bundle.putByteArray("Product image", productimage.getBytes());
intent.putExtras(bundle);
startActivity(intent);
Please try this
First convert imageview to bitmap.
product_imageView.buildDrawingCache();
Bitmap bitmap = product_imageView.getDrawingCache();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("BitmapImage", bitmap);
In second Activity please include this line:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
imageview.setImageBitmap(bitmap);
Why are you sending images between activities?, why not just place your image in drawable folder and pass the name or path of the image to the next activity and then display or do whatever you want to do with it.
You do it like this
put your image in drawable folder for example image1.png.
then all you do is parse a string with the name for the image to the second activity example:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("image", "image1.png");
startActivity(intent);
On the second activity you display for instance:
Intent intent = getIntent();
String imageStr = intent.getStringExtra("image");
ImageView imageView = (ImageView)findViewById(R.id.myimage);
imageView.setImageResource(R.drawable.image1);
I hope code is correct, forgive my mistakes I wrote this in a hurry.

.putExtra into another file

This is my code for the .putExtra:
String url = "test";
startActivity(new Intent(MainActivity.this, RelationshipRemoved.class)
.putExtra("userInfo", urlTwo));
How do I call the value urlTwo in another file?
You can receive the value in another Activity (not just another file) reading the value from the received bundle, like this:
String userInfo = getIntent().getStringExtra("userInfo");
or
String userInfo = getIntent().getExtras().getString("userInfo");
Example:
Activity sending an intent:
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putExtra("userInfo", "abcdef123456");
startActivity(intent);
ActivityTwo receive the data stored :
String receivedValue = getIntent().getStringExtra("userInfo");
the variable receivedValue will contain the string value "abcdef123456".

Intent filter for receiving intents

I have one question about android programming. I have an file explorer app and I want to implement various functions like open photos music etc. But in app itself, not sending intents to other apps! So, my question is, what intent filter I should use for "plugin" app for receiving "opening" intent ?
This is my code
public static void openFile(final Context context, final File target) {
final String mime = MimeTypes.getMimeType(target);
final Intent i = new Intent(Intent.ACTION_VIEW);
if (mime != null) {
i.setDataAndType(Uri.fromFile(target), mime);
} else {
i.setDataAndType(Uri.fromFile(target), "*/*");
}
if (context.getPackageManager().queryIntentActivities(i, 0).isEmpty()) {
Toast.makeText(context, R.string.cantopenfile, Toast.LENGTH_SHORT)
.show();
return;
}
try {
context.startActivity(i);
} catch (Exception e) {
Toast.makeText(context,
context.getString(R.string.cantopenfile) + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
ok let's assume you want to open an image from your own explorer
create a new Activity let's say ImageActivity, then you simply implement an ImageViewer inside your activity and from the calling Activity simply send an intent with an Extra Bundle containing your image Bytes
EDIT
SENDING ACTIVITY
Intent intent = new Intent(MainActivity.this, ImageActivity.class);
ImageView imageView = findViewById(R.id.image).getDrawable();
imageView.buildDrawingCache();
Bitmap image = imageView.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);
Receiving Activity ImageActivity
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
ImageView image = findViewById(R.id.image);
image.setImageBitmap(bmp );

Android getting values from intent

Below shown is the code, i am using to get values from the intent
Bundle extras = intent.getExtras();
extras object has the follwoing information
Bundle[{message=Order #400000063 is Ready for pickup, android.support.content.wakelockid=2, collapse_key=do_not_collapse, from=552489062080, e_id=364}]
When I say
extras.getString("message");
returns null. I am not sure how to get the values ?
In your Activity1:
Intent i = new Intent(Activity1.this,Activity2.class);
i.putExtra("message", "string_value");
startActivity(i);
In your Activity2:
// do a try/catch block or check
//if getIntent().getStringExtra("message") is null
String str = getIntent().getStringExtra("string_tag");
Probably you are writing different tag name ..
Use the same tag name which you used while sending data with Intent.
while sending data if you used code as
Intent i = new Intent(Activity1.this,Activity2.class);
i.putExtra("TAG_NAME", "string_value");
startActivity(i);
then use code below as
String str = getIntent().getStringExtra("TAG_NAME");
try it...

How to recive Image file with Intents?

am sending an image with help of intent as follow
Uri profileImage = Uri.parse("android.resource://Tset/res/drawable-hdpi/arr.jpeg");
details.setType("image/png");
details.putExtra(Intent.EXTRA_STREAM, profileImage);
startActivity(details);
how do i can get the image path in my receiving end activity ?
Try This
Pass image url from current activity using intent
Intent myIntent = new Intent(this, MyImageViewActivity.class);
Bundle bundle = new Bundle();
bundle.putString("image", path);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
Here path is
String path="android.resource://Tset/res/drawable-hdpi/arr.jpeg"
in your receiving activity
Bundle bundle = this.getIntent().getExtras();
String path = bundle.getString("image");
Finally i done it with help of id of the image as follow ! !
thanks for all of ur reply ! !
profilePictureID=R.drawable.image name;//name of image in drawable folder dont use extensions like.jpg and all
IntentObject.putExtra("ImageID",profilePictureID);
startActivity(IntenetObject);
In receiving activity
int pictureId=getIntent().getIntExtra("ImageID",0);
profilePicture.setImageResource(pictureId);
Try this..
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "image/png");
startActivity(intent);

Categories

Resources