Ребята,проблема следующая. У меня есть Fragment. В этом фрагменте я хочу загрузить изображение и после его отобразить на экране. Пока реализовал только загрузку, но почему-то у меня выбивает ошибку. Ошибка ссылается на эту строчку кода:
file.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
Вот весь код:
public class EditProfile extends Fragment{
private static final int GALLERY_INTENT=2;
private EditText editProfileName;
private EditText editProfilePhone;
private EditText editProfileNick;
private ImageView image;
private Button saveBtn;
int PICK_IMAGE_REQUEST = 111;
private final int LENGTH = 1000;
FirebaseUser user;
public EditProfile() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_edit_profile, container, false);
image=(ImageView)v.findViewById(R.id.edit_profile_photo);
user= FirebaseAuth.getInstance().getCurrentUser();
editProfileName=(EditText)v.findViewById(R.id.edit_profile_name);
editProfilePhone=(EditText)v.findViewById(R.id.edit_profile_phone);
saveBtn=(Button)v.findViewById(R.id.edit_profile_save);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chageProfile();
}
});
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setImage();
}
});
return v;
}
public void setImage(){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,GALLERY_INTENT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==GALLERY_INTENT && resultCode==RESULT_OK){
Uri uri = data.getData();
StorageReference storageReference=FirebaseStorage.getInstance().getReference();
StorageReference file=storageReference.child(user.getUid()).child(uri.getLastPathSegment());
file.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getActivity(),"Upload Done",Toast.LENGTH_SHORT).show();
}
});
}
}
}
Ошибки:
FATAL EXCEPTION: main
Process: by.ittech.test_chat, PID: 6769
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=-1, data=Intent { dat=content://media/external/images/media/34 }} to activity {by.ittech.test_chat/by.ittech.test_chat.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/34 from pid=6769, uid=10060 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/34 from pid=6769, uid=10060 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1104)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:795)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:749)
at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at by.ittech.test_chat.EditProfile.onActivityResult(EditProfile.java:99)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)