Media Document permission denial in android












0















I am creating a notepad application where the user can add images to his notes . When he creates a new note he can choose a fiew images and they are displayed instantly without any problems in a gridview in that activity which is inside a newnote activity . However the problem comes after. When he saves the new note the uris of the selected images are saved in an xml file as strings. When i try to to view a selected note the image paths from the xml are converted again to arraylist and get attached to a grid view. thats where the problem comes . The error is



Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{ee1d3ac 8522:aahmf.notepad/u0a89} (pid=8522, uid=10089) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs -



code for selecting images for a new note



if (data.getClipData() != null) 
{
ClipData mClipData = data.getClipData();
ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
for (int i = 0; i < mClipData.getItemCount(); i++) {

ClipData.Item item = mClipData.getItemAt(i);
Uri uri = item.getUri();
mArrayUri.add(uri);
// Get the cursor
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = cursor.getString(columnIndex);
imagesEncodedList.add(imageEncoded);
cursor.close();

galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);

}
ImagePaths= mArrayUri;


Adapter Code



public class GalleryAdapter extends BaseAdapter {

private Context ctx;
private int pos;
private LayoutInflater inflater;
private ImageView ivGallery;
ArrayList<Uri> mArrayUri;
public GalleryAdapter(Context ctx, ArrayList<Uri> mArrayUri) {

this.ctx = ctx;
this.mArrayUri = mArrayUri;
}

@Override
public int getCount() {
return mArrayUri.size();
}

@Override
public Object getItem(int position) {
return mArrayUri.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

pos = position;
inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View itemView = inflater.inflate(R.layout.gv_item, parent, false);

ivGallery = (ImageView) itemView.findViewById(R.id.ivGallery);

ivGallery.setImageURI(mArrayUri.get(position));

return itemView;
}


XMl creation code



xmlSerializer.startTag(null, "userData");
xmlSerializer.startTag(null,"Text");
xmlSerializer.text(NoteText);
xmlSerializer.endTag(null, "Text");
for(int i = 0;i<Gallery.ImagePaths.size();i++)
{
xmlSerializer.startTag(null,"Image"+i);
xmlSerializer.text(Gallery.ImagePaths.get(i).toString());
xmlSerializer.endTag(null,"Image"+i);
}
xmlSerializer.endTag(null, "userData");
xmlSerializer.endDocument();
xmlSerializer.flush();


And finally
Convert string type paths from xml to uri for showing them in the gridview



switch (eventType)
{
case XmlPullParser.START_TAG:
for (int i = 0; i <= XmlPullParser.END_TAG; i++)
{
if (tagname.equalsIgnoreCase("Image" + i))
{
try {
eventType=xpp.next();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
//eventType=XmlPullParser.TEXT;
// if (eventType == XmlPullParser.TEXT)

Images.add(Uri.parse(xpp.getText()));
galleryAdapter = new GalleryAdapter(getApplicationContext(),Images);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
//}
}


}
break;
}









share|improve this question



























    0















    I am creating a notepad application where the user can add images to his notes . When he creates a new note he can choose a fiew images and they are displayed instantly without any problems in a gridview in that activity which is inside a newnote activity . However the problem comes after. When he saves the new note the uris of the selected images are saved in an xml file as strings. When i try to to view a selected note the image paths from the xml are converted again to arraylist and get attached to a grid view. thats where the problem comes . The error is



    Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{ee1d3ac 8522:aahmf.notepad/u0a89} (pid=8522, uid=10089) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs -



    code for selecting images for a new note



    if (data.getClipData() != null) 
    {
    ClipData mClipData = data.getClipData();
    ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
    for (int i = 0; i < mClipData.getItemCount(); i++) {

    ClipData.Item item = mClipData.getItemAt(i);
    Uri uri = item.getUri();
    mArrayUri.add(uri);
    // Get the cursor
    Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
    // Move to first row
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    imageEncoded = cursor.getString(columnIndex);
    imagesEncodedList.add(imageEncoded);
    cursor.close();

    galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
    gvGallery.setAdapter(galleryAdapter);
    gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
    ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
    .getLayoutParams();
    mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);

    }
    ImagePaths= mArrayUri;


    Adapter Code



    public class GalleryAdapter extends BaseAdapter {

    private Context ctx;
    private int pos;
    private LayoutInflater inflater;
    private ImageView ivGallery;
    ArrayList<Uri> mArrayUri;
    public GalleryAdapter(Context ctx, ArrayList<Uri> mArrayUri) {

    this.ctx = ctx;
    this.mArrayUri = mArrayUri;
    }

    @Override
    public int getCount() {
    return mArrayUri.size();
    }

    @Override
    public Object getItem(int position) {
    return mArrayUri.get(position);
    }

    @Override
    public long getItemId(int position) {
    return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    pos = position;
    inflater = (LayoutInflater) ctx
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.gv_item, parent, false);

    ivGallery = (ImageView) itemView.findViewById(R.id.ivGallery);

    ivGallery.setImageURI(mArrayUri.get(position));

    return itemView;
    }


    XMl creation code



    xmlSerializer.startTag(null, "userData");
    xmlSerializer.startTag(null,"Text");
    xmlSerializer.text(NoteText);
    xmlSerializer.endTag(null, "Text");
    for(int i = 0;i<Gallery.ImagePaths.size();i++)
    {
    xmlSerializer.startTag(null,"Image"+i);
    xmlSerializer.text(Gallery.ImagePaths.get(i).toString());
    xmlSerializer.endTag(null,"Image"+i);
    }
    xmlSerializer.endTag(null, "userData");
    xmlSerializer.endDocument();
    xmlSerializer.flush();


    And finally
    Convert string type paths from xml to uri for showing them in the gridview



    switch (eventType)
    {
    case XmlPullParser.START_TAG:
    for (int i = 0; i <= XmlPullParser.END_TAG; i++)
    {
    if (tagname.equalsIgnoreCase("Image" + i))
    {
    try {
    eventType=xpp.next();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (XmlPullParserException e) {
    e.printStackTrace();
    }
    //eventType=XmlPullParser.TEXT;
    // if (eventType == XmlPullParser.TEXT)

    Images.add(Uri.parse(xpp.getText()));
    galleryAdapter = new GalleryAdapter(getApplicationContext(),Images);
    gvGallery.setAdapter(galleryAdapter);
    gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
    ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
    .getLayoutParams();
    mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
    //}
    }


    }
    break;
    }









    share|improve this question

























      0












      0








      0








      I am creating a notepad application where the user can add images to his notes . When he creates a new note he can choose a fiew images and they are displayed instantly without any problems in a gridview in that activity which is inside a newnote activity . However the problem comes after. When he saves the new note the uris of the selected images are saved in an xml file as strings. When i try to to view a selected note the image paths from the xml are converted again to arraylist and get attached to a grid view. thats where the problem comes . The error is



      Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{ee1d3ac 8522:aahmf.notepad/u0a89} (pid=8522, uid=10089) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs -



      code for selecting images for a new note



      if (data.getClipData() != null) 
      {
      ClipData mClipData = data.getClipData();
      ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
      for (int i = 0; i < mClipData.getItemCount(); i++) {

      ClipData.Item item = mClipData.getItemAt(i);
      Uri uri = item.getUri();
      mArrayUri.add(uri);
      // Get the cursor
      Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
      // Move to first row
      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      imageEncoded = cursor.getString(columnIndex);
      imagesEncodedList.add(imageEncoded);
      cursor.close();

      galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
      gvGallery.setAdapter(galleryAdapter);
      gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
      ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
      .getLayoutParams();
      mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);

      }
      ImagePaths= mArrayUri;


      Adapter Code



      public class GalleryAdapter extends BaseAdapter {

      private Context ctx;
      private int pos;
      private LayoutInflater inflater;
      private ImageView ivGallery;
      ArrayList<Uri> mArrayUri;
      public GalleryAdapter(Context ctx, ArrayList<Uri> mArrayUri) {

      this.ctx = ctx;
      this.mArrayUri = mArrayUri;
      }

      @Override
      public int getCount() {
      return mArrayUri.size();
      }

      @Override
      public Object getItem(int position) {
      return mArrayUri.get(position);
      }

      @Override
      public long getItemId(int position) {
      return 0;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

      pos = position;
      inflater = (LayoutInflater) ctx
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      View itemView = inflater.inflate(R.layout.gv_item, parent, false);

      ivGallery = (ImageView) itemView.findViewById(R.id.ivGallery);

      ivGallery.setImageURI(mArrayUri.get(position));

      return itemView;
      }


      XMl creation code



      xmlSerializer.startTag(null, "userData");
      xmlSerializer.startTag(null,"Text");
      xmlSerializer.text(NoteText);
      xmlSerializer.endTag(null, "Text");
      for(int i = 0;i<Gallery.ImagePaths.size();i++)
      {
      xmlSerializer.startTag(null,"Image"+i);
      xmlSerializer.text(Gallery.ImagePaths.get(i).toString());
      xmlSerializer.endTag(null,"Image"+i);
      }
      xmlSerializer.endTag(null, "userData");
      xmlSerializer.endDocument();
      xmlSerializer.flush();


      And finally
      Convert string type paths from xml to uri for showing them in the gridview



      switch (eventType)
      {
      case XmlPullParser.START_TAG:
      for (int i = 0; i <= XmlPullParser.END_TAG; i++)
      {
      if (tagname.equalsIgnoreCase("Image" + i))
      {
      try {
      eventType=xpp.next();
      } catch (IOException e) {
      e.printStackTrace();
      } catch (XmlPullParserException e) {
      e.printStackTrace();
      }
      //eventType=XmlPullParser.TEXT;
      // if (eventType == XmlPullParser.TEXT)

      Images.add(Uri.parse(xpp.getText()));
      galleryAdapter = new GalleryAdapter(getApplicationContext(),Images);
      gvGallery.setAdapter(galleryAdapter);
      gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
      ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
      .getLayoutParams();
      mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
      //}
      }


      }
      break;
      }









      share|improve this question














      I am creating a notepad application where the user can add images to his notes . When he creates a new note he can choose a fiew images and they are displayed instantly without any problems in a gridview in that activity which is inside a newnote activity . However the problem comes after. When he saves the new note the uris of the selected images are saved in an xml file as strings. When i try to to view a selected note the image paths from the xml are converted again to arraylist and get attached to a grid view. thats where the problem comes . The error is



      Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{ee1d3ac 8522:aahmf.notepad/u0a89} (pid=8522, uid=10089) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs -



      code for selecting images for a new note



      if (data.getClipData() != null) 
      {
      ClipData mClipData = data.getClipData();
      ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
      for (int i = 0; i < mClipData.getItemCount(); i++) {

      ClipData.Item item = mClipData.getItemAt(i);
      Uri uri = item.getUri();
      mArrayUri.add(uri);
      // Get the cursor
      Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
      // Move to first row
      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      imageEncoded = cursor.getString(columnIndex);
      imagesEncodedList.add(imageEncoded);
      cursor.close();

      galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
      gvGallery.setAdapter(galleryAdapter);
      gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
      ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
      .getLayoutParams();
      mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);

      }
      ImagePaths= mArrayUri;


      Adapter Code



      public class GalleryAdapter extends BaseAdapter {

      private Context ctx;
      private int pos;
      private LayoutInflater inflater;
      private ImageView ivGallery;
      ArrayList<Uri> mArrayUri;
      public GalleryAdapter(Context ctx, ArrayList<Uri> mArrayUri) {

      this.ctx = ctx;
      this.mArrayUri = mArrayUri;
      }

      @Override
      public int getCount() {
      return mArrayUri.size();
      }

      @Override
      public Object getItem(int position) {
      return mArrayUri.get(position);
      }

      @Override
      public long getItemId(int position) {
      return 0;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

      pos = position;
      inflater = (LayoutInflater) ctx
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      View itemView = inflater.inflate(R.layout.gv_item, parent, false);

      ivGallery = (ImageView) itemView.findViewById(R.id.ivGallery);

      ivGallery.setImageURI(mArrayUri.get(position));

      return itemView;
      }


      XMl creation code



      xmlSerializer.startTag(null, "userData");
      xmlSerializer.startTag(null,"Text");
      xmlSerializer.text(NoteText);
      xmlSerializer.endTag(null, "Text");
      for(int i = 0;i<Gallery.ImagePaths.size();i++)
      {
      xmlSerializer.startTag(null,"Image"+i);
      xmlSerializer.text(Gallery.ImagePaths.get(i).toString());
      xmlSerializer.endTag(null,"Image"+i);
      }
      xmlSerializer.endTag(null, "userData");
      xmlSerializer.endDocument();
      xmlSerializer.flush();


      And finally
      Convert string type paths from xml to uri for showing them in the gridview



      switch (eventType)
      {
      case XmlPullParser.START_TAG:
      for (int i = 0; i <= XmlPullParser.END_TAG; i++)
      {
      if (tagname.equalsIgnoreCase("Image" + i))
      {
      try {
      eventType=xpp.next();
      } catch (IOException e) {
      e.printStackTrace();
      } catch (XmlPullParserException e) {
      e.printStackTrace();
      }
      //eventType=XmlPullParser.TEXT;
      // if (eventType == XmlPullParser.TEXT)

      Images.add(Uri.parse(xpp.getText()));
      galleryAdapter = new GalleryAdapter(getApplicationContext(),Images);
      gvGallery.setAdapter(galleryAdapter);
      gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
      ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
      .getLayoutParams();
      mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
      //}
      }


      }
      break;
      }






      java android android-studio uri android-permissions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 23:54









      StarkSlasherStarkSlasher

      185




      185
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384326%2fmedia-document-permission-denial-in-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384326%2fmedia-document-permission-denial-in-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          Npm cannot find a required file even through it is in the searched directory

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith