In my app I need to transfer a Uri image from my first Activity to another. I know how to send a Bitmap through an intent. I'm a bigginer programmer so I don't know what would be better to do: transfer the Uri with an intent or change the Uri to a Bitmap then send that?
use with putExtra to send the Uri Path:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setClass(ThisActivity.this, NewActivity.class);
intent .putExtra("KEY", Uri);
startActivity(intent );
In the newActivity OnCreate method:
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("KEY")) {
Uri= extras.getString("KEY");
}
Use those func:
Uri to String:
Uri uri;
String stringUri;
stringUri = uri.toString();
String to Uri:
Uri uri;
String stringUri;
uri = Uri.parse(stringUri);
To avoid the error you are getting, in the code given by Miki franko, replace the line :
Uri= extras.getString("KEY");
with :
uri= Uri.parse(extras.getString("KEY"));
This is just to make the code work as I think you didn't understand what Miki tried to explain through the code.
Keep us posted if you get it resolved now.
//First Activity to get a Uri
String uri_Str = Uri.toString();
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent .putExtra("uri_Str", uri_Str);
startActivity(intent);
//Second Activity get a Uri path
Bundle b = getIntent().getExtras();
if (b != null) {
String uri_Str= b.getString("uri_Str");
Uri uri = Uri.parse(uri_Str);
}
First Activity
Uri uri = data.getData();
Intent intent=new Intent(Firstclass.class,secondclass.class);
intent.putExtra("imageUri", uri.toString());
startActivity(intent);
Second class
Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo);
Bundle extras = getIntent().getExtras();
myUri = Uri.parse(extras.getString("imageUri"));
iv_photo.setImageURI(myUri);
In your first class, you can pass the image uri like this:
Intent intent = new Intent();
intent.putExtra("your_key", imageUri.toString());
startActivity(intent);
And in the second or receiver activity, you can access the image uri this way:
Bundle extras = getIntent().getExtras();
if(extras != null){
Uri imageUri = Uri.parse(extras.getString("your_key"));
}
Related
I've tried a couple different ways to show a chosen image from the gallery in a imageView. In the MainActivity I let the user choose an Image from Gallery and use an intent in the MainActivity, to send the uri of the image to the NextActivity and set the uri to the imageView, but it's not working. I can't see the Image. In the debug mode I see the uri in the variable which I set to the ImageView but it doesn't show the image.
My code
MainActivity.java onCreate method
<!-- language: java -->
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri = data.getData();
Intent intent = new Intent (this, NextActivity.class );
if(imageUri != null){
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
}
}
NextActivity.java onCreate method
<!-- language: java -->
ImageView imgView = (ImageView) findViewById(R.id.imageView1);
Intent intent = getIntent();
String image_path = intent.getStringExtra("imageUri");
Uri fileUri = Uri.parse(image_path);
imgView.setImageURI(fileUri);
I've tried to send the Uri as it is with intent, converted as a String and reconverted and so on. I don't know.
NextActivity XML
<!-- language: xml -->
<ImageView
android:id="#+id/imageView1"
android:layout_width="396dp"
android:layout_height="450dp"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.466"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Try this:
Uri fileUri = Uri.parse(image_path);
imgView.setImageURI(null);
imgView.setImageURI(fileUri);
Ideally, this would be just one activity (perhaps using two fragments), rather than two activities.
But, if you want to stick with this structure, you will need to change this:
imageUri = data.getData();
Intent intent = new Intent (this, NextActivity.class );
if(imageUri != null){
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
}
to:
imageUri = data.getData();
if (imageUri != null){
Intent intent = new Intent (this, NextActivity.class);
intent.setData(imageUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
and change:
String image_path = intent.getStringExtra("imageUri");
Uri fileUri = Uri.parse(image_path);
to:
Uri fileUri = intent.getData();
Without that, NextActivity will not have rights to use the content identified by the Uri.
Also, please replace setImageURI() with an image-loading library, such as Glide or Picasso.
The uri nul, i don't know why... Please help
#Override
protected void onHandleIntent(Intent intent) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Uri uri = intent.getData();
Intent action = new Intent(this, AddReminderActivity.class);
action.setData(uri);
PendingIntent operation = TaskStackBuilder.create(this)
.addNextIntentWithParentStack(action)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
if (uri != null) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
} else {
Log.i(TAG, "+OOOOOOOOONUMERO Uriiiii NuLL");
}
}
Uri might be present in getExtras() not in getData()
So you should use it like below:-
Bundle extras = intent.getExtras();
You can print the extras and see what KEY you should be used to fetch the uri. The following lines of code will print the entire content if a intent extras.
Find out key from the logs and use it.
Log.d("intent URI", intent.toUri(0));
And finally you can get use it like this:-
Uri uri = (Uri) extras.get(KEY);
I got a code for pick a picture from my device gallery , code is like this i used.
Intent getintent = new Intent(Intent.ACTION_GET_CONTENT);
getintent.setType("image/*");
Intent pickintent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickintent.setType("image/*");
Intent chooserintent = Intent.createChooser(getintent, "Select Image");
chooserintent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickintent});
startActivityForResult(chooserintent, SELECT_PICTURE);
chooserintent.putExtra("data", SELECT_PICTURE);
Can i put a picture as a extra to send it to another intent? this is my code for 2nd page to show the picture that i choose in 1st Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showpic2);
findViewById(R.id.imageButton); // Replace with id of your button.
Intent intent = getIntent();
Bitmap gambar = intent.getData("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(gambar);
}
I am getting error with this code, can someone help me? thanks guys !!
sorry for my bad english.
You don't need to put the actual image's bitmap into an intent. After coming back from gallery you will be given an Uri. This uri points to the selected image. To display this image on another activity, what you should do is to only pass that Uri to the second activity.
First Activity's onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_ID && resultCode == RESULT_OK && data != null){
Uri uri = data.getData();
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("imageUri", uri);
startActivity(intent);
}
}
And in the second Activity's onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showpic2);
Uri uri = intent.getParcelableExtra("imageUri");
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageUri(uri);
}
i have an image file path stored in my database. Now using it i want to open images from SD Card in my gallery. How can i do that. I have seen methods from Uri and using this method but i am getting error
File file = new File(filename);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setType("image/*");
startActivity(intent); /** replace with your own uri */
How to fix it,
04-20 08:42:23.516: E/AndroidRuntime(16815): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/My%20App/image_umxto_1.jpg }
best regards
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(filename)), "image/*");
startActivity(intent);
Use this code and tell me
You can also use the below code:::
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
startActivity(intent);
you can use the below line to do your task:::
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
I was getting FileUriExposedException and after some digging found solution. Convert file path to URI and pass it in intent.
MediaScannerConnection.scanFile(getContext(),
new String[] { getImagePath() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("onScanCompleted", uri.getPath());
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
});
Hope it helps.
I'm trying to pass a URI-Object to my Intent in order to use that URI
in another activity.
How do I pass a URI?
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra("imageUri", imageUri);
startActivity(intent);
this.finish();
How do I use now this URI in my other activity?
imageUri = extras.getString("imageUri"); // I know thats wrong ...
you can store the uri as string
intent.putExtra("imageUri", imageUri.toString());
and then just convert the string back to uri like this
Uri myUri = Uri.parse(extras.getString("imageUri"));
The Uri class implements Parcelable, so you can add and extract it directly from the Intent
// Add a Uri instance to an Intent
intent.putExtra("imageUri", uri);
// Get a Uri from an Intent
Uri uri = intent.getParcelableExtra("imageUri");
You can use the same method for any objects that implement Parcelable, and you can implement Parcelable on your own objects if required.
In Intent, you can directly put Uri. You don't need to convert the Uri to string and convert back again to Uri.
Look at this simple approach.
// put uri to intent
intent.setData(imageUri);
And to get Uri back from intent:
// Get Uri from Intent
Uri imageUri=getIntent().getData();
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
this.finish();
And then you can fetch it like this:
imageUri = Uri.parse(extras.getString("imageUri"));
here how I use it;
This button inside my CameraActionActivity Activity class where I call camera
btn_frag_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intenImatToSec = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intenImatToSec, REQUEST_CODE_VIDEO);
//intenImatToSec.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//intenImatToSec.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
//Toast.makeText(getActivity(), "Hello From Camera", Toast.LENGTH_SHORT).show();
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_IMG) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
Intent intentBitMap = new Intent(getActivity(), DisplayImage.class);
// aldıgımız imagi burda yonlendirdiğimiz sınıfa iletiyoruz
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intentBitMap.putExtra("byteArray", _bs.toByteArray());
startActivity(intentBitMap);
} else if (requestCode == REQUEST_CODE_VIDEO) {
Uri videoUrl = data.getData();
Intent intenToDisplayVideo = new Intent(getActivity(), DisplayVideo.class);
intenToDisplayVideo.putExtra("videoUri", videoUrl.toString());
startActivity(intenToDisplayVideo);
}
}
}
And my other DisplayVideo Activity Class
VideoView videoView = (VideoView) findViewById(R.id.videoview_display_video_actvity);
Bundle extras = getIntent().getExtras();
Uri myUri= Uri.parse(extras.getString("videoUri"));
videoView.setVideoURI(myUri);
If you want to use standard extra data field, you would do something like this:
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra(Intent.EXTRA_STREAM, imageUri.toString());
startActivity(intent);
this.finish();
The documentation for Intent says:
EXTRA_STREAM added in API level 1
String EXTRA_STREAM
A content: URI holding a stream of data associated with the Intent,
used with ACTION_SEND to supply the data being sent.
Constant Value: "android.intent.extra.STREAM"
You don't have to use the built-in standard names, but it's probably good practice and more reusable. Take a look at the developer documentation for a list of all the built-in standard extra data fields.
The Uri.parse(extras.getString("imageUri")) was causing an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable)' on a null object reference
So I changed to the following:
intent.putExtra("imageUri", imageUri)
and
Uri uri = (Uri) getIntent().get("imageUri");
This solved the problem.
you can do like this. imageuri can be converted into string like this.
intent.putExtra("imageUri", imageUri.toString());