0

I would like to take a photo from gallery, I use this Code.

The code work for some photo in sumsung s4 device but it doesn't work in sumsung a7.

I want to create a code that, will work for all device.

public class SignUpPersonalUserStep5 extends Fragment {


public SignUpPersonalUserStep5(PersonalProfil profil) {
    this.profil = profil;
}

PersonalProfil profil;
public static final int RESULT_LOAD_PHOTO = 3;
ImageView signUpPhoto;
String ImageDecodableString;
FloatingActionButton button;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.step5_sign_up, container, false);
    signUpPhoto = (ImageView) view.findViewById(R.id.signUpPhoto);
    signUpPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_PHOTO);

        }
    });

    button = (FloatingActionButton) view.findViewById(R.id.step5Button);

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

            FragmentManager manager = getActivity().getSupportFragmentManager();

            SignUpPersonalUserStep4 signUpStep4 = new SignUpPersonalUserStep4(profil);
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.addToBackStack("");
            transaction.setCustomAnimations(R.anim.toastox_sheet_show, R.anim.toastox_sheet_hide);
            transaction.replace(R.id.signUpForFragment, signUpStep4);
            transaction.commit();


        }
    });

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_PHOTO && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            // Get the cursor
            Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            ImageDecodableString = cursor.getString(columnIndex);
            cursor.close();
            Log.d("nizar",ImageDecodableString);
            signUpPhoto.setImageBitmap(BitmapFactory
                    .decodeFile(ImageDecodableString));
            //  ImageDecodableString = Base64.encodeToString(ImageDecodableString.getBytes(), Base64.DEFAULT);
            profil.setImage(ImageDecodableString);

        }
    } catch (Exception e) {
        Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }


}}

I want to say that the data is not empty.
Please let me know what you think
Thank you very much!

Jainish Kapadia
  • 2,603
  • 5
  • 18
  • 29

3 Answers3

1

Try this code:

private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
        } else {
            Intent intent = new Intent();
            intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, PICK_IMAGE_REQUEST);
        }
    }
0

If you are targeting Android SDK >=23, you need to take permission at run-time by user if user having Android OS Marshmallow or greater.

https://developer.android.com/training/permissions/requesting.html

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
0

This brings up the Documents app. To allow the user to also use any gallery apps they might have installed:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getIntent.setType("image/*");
    Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    pickIntent.setType("image/*");
    Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
    startActivityForResult(chooserIntent, PICK_IMAGE);

more details here

Community
  • 1
  • 1
Hemanth S
  • 669
  • 6
  • 16
  • this worked to me Uri Selected_Image_Uri=data.getData(); ImageView imageView= (ImageView) findViewById(R.id.loadedimg); imageView.setImageURI(Selected_Image_Uri); thanks – Nizar Abdelhedi Feb 09 '17 at 12:57