How to transfer a Uri image from one activity to a

2019-02-16 01:26发布

问题:

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?

回答1:

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);


回答2:

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.



回答3:

        //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);
        }


回答4:

  1. First Activity

     Uri uri = data.getData();
            Intent intent=new Intent(Firstclass.class,secondclass.class);
            intent.putExtra("imageUri", uri.toString());
            startActivity(intent);
    
  2. 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);