Android中的媒体文档权限拒绝

时间:2018-11-19 23:54:40

标签: java android android-studio uri android-permissions

我正在创建一个记事本应用程序,用户可以在其中将图像添加到他的记事本中。当他创建新笔记时,他可以选择少量图像,这些图像将立即显示,而在newnote活动内部的活动的gridview中不会出现任何问题。但是问题出在后面。当他保存新笔记时,选定图像的uris作为字符串保存在xml文件中。当我尝试查看选定的注释时,来自xml的图像路径再次转换为arraylist并附加到网格视图。那就是问题所在。错误是

拒绝权限:从ProcessRecord {ee1d3ac 8522:aahmf.notepad / u0a89}(pid = 8522,uid = 10089)打开提供程序com.android.providers.media.MediaDocumentsProvider要求您使用ACTION_OPEN_DOCUMENT或相关API进行访问- / p>

用于选择新笔记图像的代码

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;

适配器代码

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创建代码

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();

最后 将字符串类型的路径从xml转换为uri,以便在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;
                }

0 个答案:

没有答案