У меня есть uri
ссылка в моем firebaseDatabase
, сохраненная как:
content://com.android.providers.media.documents/document/audio%3A6581
Мне нужно сохранить этот файл в firebaseStorage
, предоставив эту ссылку в качестве ссылки и без обычного шаблона Intent.ACTION_GET_CONTENT
.
Но я продолжал получать эту ошибку:
Process: com.app, PID: 3773
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{3daf6d5d0 3773:com.app/u0a298} (pid=3773, uid=10298) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
Я также пытался дать ей постоянные разрешения следующим образом:
context.getContentResolver().takePersistableUriPermission(sourceTreeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Но я получил эту ошибку.
java.lang.SecurityException: No persistable permission grants found for UID 10298 and Uri content://com.android.providers.media.documents/document/audio:6581 [user 0]
Активность
private void chooseAudios() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
//intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select Audio"), PICK_AUDIO_FILE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_AUDIO_FILE && resultCode == RESULT_OK) {
if(data.getData() != null){
Uri fileUri = data.getData();
fileName = getFileName(fileUri);
fileDuration = getFileDuration(getContext(), fileUri);
if (getContext() != null){
fileSize = getFileSize(getContext(), fileUri);
}
DatabaseReference mFirebaseDatabase = databaseReference.child(userID).push();
Map<String, Object> map = new HashMap<>();
map.put("title", fileName);
map.put("size", fileSize);
map.put("time", fileDuration);
map.put("status", "queue");
map.put("pushKey", Objects.requireNonNull(mFirebaseDatabase.getKey()));
map.put("urlString", fileUri.toString());
map.put("key1", "");
map.put("key2", "");
mFirebaseDatabase.setValue(map).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
updateLayout();
} else {
updateLayout();
Toast.makeText(getContext(), task.getException().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
Адаптер
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_audio_draft_item, parent, false);
mAuth = FirebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Podcasts");
databaseReferenceDraft = FirebaseDatabase.getInstance().getReference().child("AudioDraft");
storageReference = FirebaseStorage.getInstance().getReference();
context = parent.getContext();
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
AudioDraft audioDraft = postList.get(position);
String time = audioDraft.getTime();
String title = audioDraft.getTitle();
String size = audioDraft.getSize();
String status = audioDraft.getStatus();
String key1 = audioDraft.getKey1();
String key2 = audioDraft.getKey2();
String uriString = audioDraft.getUrlString();
String pushKey = audioDraft.getPushKey();
holder.fileNameView.setText(title);
holder.fileSizeView.setText(size+" MB");
holder.fileDurationView.setText(time);
FirebaseUser firebaseUser = mAuth.getCurrentUser();
if(firebaseUser!=null){
user_id = firebaseUser.getUid();
}
databaseReferenceDraft.child(user_id).child(pushKey).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String status = dataSnapshot.child("status").getValue(String.class);
if (status != null && status.equals("download")) {
final StorageReference postPath = storageReference.child("Audios").child(String.valueOf(System.currentTimeMillis()));
mUploadTask = (UploadTask) postPath.putFile(Uri.parse(uriString)).addOnProgressListener(taskSnapshot -> {
//calculating progress percentage
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//displaying percentage in progress dialog
holder.donutProgress.setDonut_progress(String.valueOf(progress));
});
Task<Uri> urlTask = mUploadTask.continueWithTask(task -> {
if (!task.isSuccessful()) {
Toast.makeText(activity, task.getException().toString(), Toast.LENGTH_SHORT).show();
}
// Continue with the task to get the download URL
return postPath.getDownloadUrl();
}).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Uri downloadURI = task.getResult();
if (downloadURI != null) {
downloadUri = downloadURI.toString();
Map<String, Object> map = new HashMap<>();
map.put("downloadURL", downloadUri);
databaseReference.child(user_id).child(key1).child(key2).updateChildren(map).addOnSuccessListener(aVoid -> {
Toast.makeText(context, "success", Toast.LENGTH_SHORT).show();
databaseReferenceDraft.child(user_id).child(pushKey).removeValue();
});
}
} else {
Toast.makeText(activity, Objects.requireNonNull(task.getException()).toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});