Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can get Uri from intent?

I did

@Override
public void onClick(View v) {
    switch (v.getId()){
        //если выбрали камеру - запускаем ее
        case R.id.b_camera:
            //uri = generateFileUri();
            if (uri == null) {
                Toast.makeText(getView().getContext(), getResources().getString(R.string.sdnot), Toast.LENGTH_LONG).show();
                return;
            }
            Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            //intentCamera.putExtra("return-data", true);
            startActivityForResult(intentCamera, PHOTO_INTENT_REQUEST_CODE);
        break;
    }
}

Method generateFileUri() is called in onCreateView(). Line intentCamera.putExtra("return-data", true); doesnot work for me (if use "return-data" as a key in onActivityResult at getParceble()).

@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturned) {

    switch (requestCode) {

        //если результат пришел от камеры
        case PHOTO_INTENT_REQUEST_CODE:
            if (resultCode == getActivity().RESULT_OK) {
                Log.i("HHHHHHHHHHHH", "result ok");
                //извлекаем uri фотки из интента
                Uri selectedImage = imageReturned.getData();
                Log.i("HHHHHHHHHHHH", "getdata works!");
                //создаем интент для запуска новой активити
                Intent last_intent_photo = new Intent(getView().getContext(), ViewPhoto.class);
                //помещаем в интент этот uri
                last_intent_photo.putExtra("fotka",selectedImage);
                //стартуем новую активити
                startActivity(last_intent_photo);

            } else if (resultCode == getActivity().RESULT_CANCELED)
                Toast.makeText(getView().getContext(), "Capture cancelled", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(getView().getContext(), "Capture failed", Toast.LENGTH_LONG).show();
                break;
        default: super.onActivityResult(requestCode, resultCode, imageReturned);

    }
}

I see log "result ok". I think it meens camera did work well. But then i see: Failure delivering result ResultInfo{who=android:fragment:2, request=100, result=-1, data=null}. How it can be?!

Well, i tried Uri selectedImage = imageReturned.getExtras().getParcelable(); but i have not a key in my intent to put it into getParcelable(String key).

So, why result is ok, but data=null?

like image 757
Evgeny GooDi Avatar asked Dec 18 '22 17:12

Evgeny GooDi


2 Answers

Option 1:

//Uri myUri = ...
intent.setData(myUri);

and

Uri uri = intent.getData();

Option 2:

According to documentation:

toString() - Returns the encoded string representation of this URI

//Uri myUri = ...
intent.putStringExtra("uri",myUri.toString());

and

String uriString = intent.getStringExtra("uri");
Uri uri = Uri.parse(uriString);
like image 145
WiseTap Avatar answered Jan 08 '23 23:01

WiseTap


why do you need to get the uri from intert since you are passing it youself? just use the same uri as:

intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, uri);

UPD to clarify:

when you're starting the cameta with option:

intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, uri);

you are telling to the camera that the photo must be stored in this path. so, if the result OK you need to get that photo by this uri.

 if (resultCode == getActivity().RESULT_OK) {
    Log.i("HHHHHHHHHHHH", "result ok");

    // same as passed in intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    Uri selectedImage = uri; 

    Log.i("HHHHHHHHHHHH", "getdata works!");
    //создаем интент для запуска новой активити
    Intent last_intent_photo = new Intent(getView().getContext(), ViewPhoto.class);
    //помещаем в интент этот uri
    last_intent_photo.putExtra("fotka",selectedImage);
    //стартуем новую активити
    startActivity(last_intent_photo);
} 

UPD 2

FragmentScaling.onActivityCreated:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (getActivity().getIntent().hasExtra("uri")) {
        uri = (Uri)getActivity().getIntent().getParcelableExtra("uri");
    }
}
like image 24
Oleg Khalidov Avatar answered Jan 08 '23 22:01

Oleg Khalidov