0

I implemented 3 tab in my MainActivity. 1st tab is Images fragment in which 2 buttons is implemented. 1st is to open camera and 2nd is to open gallery. I want to display that captured or selected image in new Activity called Upload Activity.

Images Fragment

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    //  return inflater.inflate(R.layout.fragment_images, container, false);
    View v = inflater.inflate(R.layout.fragment_images, container, false);
    FloatingActionButton btnCamera = (FloatingActionButton) v.findViewById(R.id.btnCamera);
    FloatingActionButton btnFolder = (FloatingActionButton) v.findViewById(R.id.btnFolder);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            File imageFolder = new File(Environment.getExternalStorageDirectory(), "/My Children");
            imageFolder.mkdir();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
            String timestamp = simpleDateFormat.format(new Date());
            File image = new File(imageFolder, timestamp+ ".jpg");
            //    Uri uriImage = Uri.fromFile(image);
            Uri uriImage = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID +  ".provider", image);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
            getActivity().startActivityForResult(intent, TAKE_PICTURE);
        }
    });
    btnFolder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            getActivity().startActivityForResult(intent, SELECT_PICTURE);
        }
    });
    return v;

}
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 try {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
    Bundle extras = data.getExtras();
    Bitmap cam = (Bitmap) extras.get("data");
    Intent i = new Intent().setClass(getActivity(), Upload.class);
    i.putExtra("image", cam);
    startActivity(i);
}
if (requestCode == SELECT_PICTURE && requestCode == RESULT_OK) {
    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    Intent i = new Intent(getActivity(), Upload.class);
    i.putExtra("image", bitmap);
    startActivity(i);
}
}catch (Exception e){
e.printStackTrace();
}

Also added this code in MainActivity

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.vp_pages + ":" + vp_pages.getCurrentItem() );
    if (page != null){
        page.onActivityResult(requestCode, resultCode, data);
    }
}

This is Upload class where i want display image

public class Upload extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload);

    Button btnUpload = (Button) findViewById(R.id.btnUpload);

    ImageView imageview = (ImageView) findViewById(R.id.imageview);
    Intent intent = getIntent();
    Bitmap bitmap = (Bitmap) intent.getParcelableExtra("image");
    imageview.setImageBitmap(bitmap);
 }
 }

After captring or selecting image, the Images fragment is opening instead of Upload Activity.

1 Answers1

0

since you are launching your camera or gallery image capture from image fragment then you can launch the Intent from there only.

But the problem is that when you set the

intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);

the data in onActivityResult will be null as you have given your own uri. so for camera request you can check with the temp uri for camera by storing it globally.

Uri cameraUri;

and then

cameraUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID +  ".provider", image);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
            getActivity().startActivityForResult(intent, TAKE_PICTURE);

at this time the uri is referring to some temporary file. Camera just clicks and override the file to be valid one.

so now get the result like below.

@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
         Intent i = new Intent().setClass(getActivity(), Upload.class);
         //here pass the original uri that we stored while starting camera
         i.putExtra("image", cameraUri.toString());
         startActivity(i);
      }
      if (requestCode == SELECT_PICTURE && requestCode == RESULT_OK) {
         Intent i = new Intent(getActivity(), Upload.class);
         //this will return you data in case of open gallery
         i.putExtra("image", data.getData().toString());
         startActivity(i);
     }
    }

now on another activity just receive this and display

public class Upload extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload);

    Button btnUpload = (Button) findViewById(R.id.btnUpload);

    ImageView imageview = (ImageView) findViewById(R.id.imageview);
    Intent intent = getIntent();
    if(intent==null){
     return;
    }
    Uri imageUri = Uri.parse(intent.getStringExtra("image"));
    imageview.setImageURI(imageUri);
 }
 }
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • Thanxx bro.. its working properly. Now i want little help. I write the code to upload this image to to the server but it is for bitmap, and now we are using Uri. so just give me a little hint how can i upload using volley? –  Jan 11 '18 at 05:37
  • I want to upload from Upload activity where image is displayed and there is one button if user want to upload. –  Jan 11 '18 at 06:18
  • you have the uri just create a file using uri and upload it. https://stackoverflow.com/questions/2975197/convert-file-uri-to-file-in-android – vikas kumar Jan 11 '18 at 06:52