Images Not Selectable in Photo Gallery

Viewed 33

I have recently made it so users can choose an image from their photo gallery or downloads and upload it to Firebase Storage and use the image as their profile picture.

The issue is whenever a user goes into their gallery, all the photos are greyed out so they cannot select them to upload. I am assuming this is an issue with permissions, but I believe I have all the correct permissions in place to avoid this issue. This is the code being used to open the gallery, choose a photo, and upload a photo to the Firebase Storage

public class AccountSettings extends AppCompatActivity {

StorageReference storageReference;
private static final int IMAGE_REQUEST = 1;
private StorageTask uploadTask;
private Uri profileImage;

private void openImage() {
    Intent intent = new Intent();
    intent.setType("image/");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, IMAGE_REQUEST);
}

private String getFileExtension(Uri uri) {
    ContentResolver contentResolver = AccountSettings.this.getContentResolver();
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    return mimeTypeMap.getMimeTypeFromExtension(contentResolver.getType(uri));
}

private void uploadImage() {
    final ProgressDialog pd = new ProgressDialog(AccountSettings.this);
    pd.setMessage("Uploading");
    pd.show();

    if (profileImage != null) {
        final StorageReference fileReference = storageReference.child(System.currentTimeMillis() + "." + getFileExtension(profileImage));

        uploadTask = fileReference.putFile(profileImage);
        uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }
                return fileReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    String mUri = downloadUri.toString();

                    HashMap<String, Object> map = new HashMap<>();
                    map.put("imageURL", mUri);
                    HomePage.myRef.updateChildren(map);

                    pd.dismiss();
                } else {
                    Toast.makeText(AccountSettings.this, "FAILED!!", Toast.LENGTH_SHORT).show();
                    pd.dismiss();
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(AccountSettings.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                pd.dismiss();
            }
        });
    } else {
        Toast.makeText(AccountSettings.this, "No image selected", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null) {
        profileImage = data.getData();

        if (uploadTask != null && uploadTask.isInProgress()) {
            Toast.makeText(AccountSettings.this, "Upload in progress", Toast.LENGTH_SHORT).show();
        } else {
            uploadImage();
        }
    }
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}
}

And these are the permissions I have in my Manifest to allow users to choose images - I believe the problem lies here but I am not positive

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

I do not know why the images are not selectable

0 Answers
Related