This problem happens when it calls the method onBindViewHolder()
and try to use setImageURI()
method in the recycler view adapter... this problem isn't appearing when i try to pick image from "Gallery" but it appears when i try to pick an image from "google photos" and "file chooser"...
Loagcat:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{14ea0f0 13163:com.application.employees/u0a175} (pid=13163, uid=10175) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
at android.os.Parcel.createExceptionOrNull(Parcel.java:2373)
at android.os.Parcel.createException(Parcel.java:2357)
at android.os.Parcel.readException(Parcel.java:2340)
at android.os.Parcel.readException(Parcel.java:2282)
at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:5702)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:6813)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2935)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:2481)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1967)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1921)
at android.graphics.ImageDecoder$ContentResolverSource.createImageDecoder(ImageDecoder.java:274)
at android.graphics.ImageDecoder.decodeDrawableImpl(ImageDecoder.java:1758)
at android.graphics.ImageDecoder.decodeDrawable(ImageDecoder.java:1751)
at android.widget.ImageView.getDrawableFromUri(ImageView.java:1011)
at android.widget.ImageView.resolveUri(ImageView.java:980)
at android.widget.ImageView.setImageURI(ImageView.java:557)
at androidx.appcompat.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:132)
at com.application.employees.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:36)
at com.application.employees.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:17)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:561)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at androidx.coordinatorlayout.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1213)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:899)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:919)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
2022-09-02 14:57:14.075 13163-13163/com.application.employees E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:784)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3470)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2938)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:7155)
at com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:7594)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2381)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2883)
at android.os.Binder.execTransactInternal(Binder.java:1154)
Pick image code:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "choose picture"), PICK_IMAGE_REQUEST_CODE);
In the same class onActivityResult()
method:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST_CODE && data!=null){
String image = String.valueOf(data.getData());
choose_image.setImageURI(Uri.parse(image));
Toast.makeText(this, "image loaded", Toast.LENGTH_SHORT).show();
}
}
Note: By the way i tried to use both ACTION_GET_CONTENT and ACTION_OPEN_DOCUMENT but both gave me the same error...
RecyclerAdapter:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> {
private ArrayList<Employee> employees;
public RecyclerAdapter(ArrayList<Employee> employees) {
this.employees = employees;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.employee_custom_item, null, false);
Holder holder = new Holder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
Employee employee = employees.get(position);
holder.image.setImageURI(Uri.parse(employee.getImg()));
holder.name.setText(employee.getName());
holder.age.setText(String.valueOf(employee.getAge()));
holder.hour.setText(String.valueOf(employee.getHour()));
}
@Override
public int getItemCount() {
return employees.size();
}
class Holder extends RecyclerView.ViewHolder{
ImageView image;
TextView name, age, hour;
public Holder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.employee_picture);
name = itemView.findViewById(R.id.employee_name);
age = itemView.findViewById(R.id.employee_age);
hour = itemView.findViewById(R.id.employee_hour);
}
}
}
Help pls ...