我正在尝试压缩图像,然后将其上传到Firebase存储。我迷恋发现的this tutorial on youtube。我被告知遵循,但是当我测试所有应用程序时,该应用程序停止运行,并且收到此错误消息,提示Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=8478, uid=10163 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
。我已经添加了permisson来读取外部存储,所以我不确定这是怎么回事。有人可以帮我解决这个问题吗?
//Android manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
//AllinoneActivity.java
final ProgressDialog progressDialog = new ProgressDialog(AllInOneActivity.this);
progressDialog.setTitle("Uploading");
progressDialog.show();
if ( filepath==null) {
Toast.makeText(AllInOneActivity.this, "All Fields required", Toast.LENGTH_LONG).show();
} else {
File file=new File(SiliCompressor.with(AllInOneActivity.this).compress(FileUtils.getPath(AllInOneActivity.this,filepath),new File(AllInOneActivity.this.getCacheDir(),"temp")));
Uri uri=Uri.fromFile(file);
storageRef.child("images/").child(anstronCoreHelper.getFileNameFromUri(uri)).putFile(filepath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(AllInOneActivity.this, "Uploaded successfully", Toast.LENGTH_LONG).show();
taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String download_url = uri.toString();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("images");
final DatabaseReference update = rootRef.child(uid);
String uploadId=update.push().getKey();
update.child(uploadId).child("Image").setValue(download_url);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(AllInOneActivity.this, "Failed to upload", Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int) progress + "%");
}
});
}