How to pass a URI to an intent?

前端 未结 8 701
日久生厌
日久生厌 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:26

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

    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.

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