图片库中无法选择的图像

时间:2019-01-12 23:11:37

标签: java android permissions firebase-storage

我最近做到了这一点,因此用户可以从照片库中选择图像或下载图像,然后将其上传到Firebase Storage并将其用作个人资料图片。

问题是,每当用户进入自己的画廊时,所有照片都会显示为灰色,因此他们无法选择要上传的照片。我假设这是权限问题,但是我相信我拥有所有正确的权限来避免此问题。这是用于打开图库,选择照片并将照片上传到Firebase存储的代码。

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);
    }
}
}

这些是我在清单中所拥有的允许用户选择图像的权限-我相信问题出在这里,但我不是肯定的

<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" />

我不知道为什么无法选择图像

0 个答案:

没有答案