How to pass a URI to an intent?

前端 未结 8 700
日久生厌
日久生厌 2020-11-27 11:50

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         


        
相关标签:
8条回答
  • 2020-11-27 12:03

    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.

    0 讨论(0)
  • 2020-11-27 12:04
    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"));
    
    0 讨论(0)
  • 2020-11-27 12:06

    you can do like this. imageuri can be converted into string like this.

    intent.putExtra("imageUri", imageUri.toString());

    0 讨论(0)
  • 2020-11-27 12:22

    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();
    
    0 讨论(0)
  • 2020-11-27 12:25

    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"));
    
    0 讨论(0)
  • 2020-11-27 12:26

    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.

    0 讨论(0)
提交回复
热议问题