I can't get the photos saved to the SD

Viewed 30

I am starting to program in Android and I am testing some functionalities. now I'm trying to get an image from the gallery and save it to a byte array. The problem is that the code that I leave below, works for images saved in the internal memory but not for those that are in the SD, I already spent 3 hours searching to see what it could be but nothing worked for now. Thank you and have a good day!

In the manifest I have this line

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

and the fragment has this code:

public class PhotoFragment extends Fragment {

   
    private ImageButton buttonphoto;
    private Context mContext;
    private  byte[] bytes;

    public void getPhoto(){
        Intent photoPicker = new Intent(Intent.ACTION_GET_CONTENT);
        photoPicker.setType("image/*");
        startActivityForResult(photoPicker, 2);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 2){
            if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                getPhoto();
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_report, container, false);

       
        buttonphoto = v.findViewById(R.id.fragmentreportesCameraButton);
        buttonphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ActivityCompat.checkSelfPermission(getContext(),Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},2);
                }else{
                    getPhoto();
                }
            }
        });
        
        return  v;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Uri images = data.getData();
        if(requestCode == 2 && resultCode == Activity.RESULT_OK && data !=null){
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),images);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                Toast.makeText(mContext,"agarro?",Toast.LENGTH_SHORT).show();
                bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
                bytes = stream.toByteArray();
                
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
    }
}
1 Answers

Android Level 10,11 changing your privacy policy. In this policy we cannot get data directly then add this tag in the manifest file inside the application tag:

android:requestLegacyExternalStorage="true"
Related