当前位置: 首页 > 知识库问答 >
问题:

如何实现为Gmail、Facebook、Evernote等提供图像的ContentProvider

程旭尧
2023-03-14
    null
public class PictureContentProvider extends ContentProvider implements AutoAnimate {
    public static final Uri CONTENT_URI = Uri.parse("content://com.enigmadream.picturecode.snapshot/picture.png");
    private static String[] mimeTypes = {"image/png"};
    private Uri generatedUri;

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
       throw new RuntimeException("PictureContentProvider.delete not supported");
    }

    @Override
    public String getType(Uri uri) {
       return "image/png";
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
       throw new RuntimeException("PictureContentProvider.insert not supported");
    }

    @Override
    public boolean onCreate() {
       generatedUri = Uri.EMPTY;
       return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
          String[] selectionArgs, String sortOrder) {
       long fileSize = 0;
       MatrixCursor result = new MatrixCursor(projection);
       File tempFile;
       try {
          tempFile = generatePictureFile(uri);
          fileSize = tempFile.length();
       } catch (FileNotFoundException ex) {
          return result;
       }
       Object[] row = new Object[projection.length];
       for (int i = 0; i < projection.length; i++) {

          if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.DISPLAY_NAME) == 0) {
             row[i] = getContext().getString(R.string.snapshot_displaystring);
          } else if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.SIZE) == 0) {
             row[i] = fileSize;
          } else if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.DATA) == 0) {
             row[i] = tempFile;
          } else if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.MIME_TYPE)==0) {
             row[i] = "image/png";
          }
       }

       result.addRow(row);
       return result;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
          String[] selectionArgs) {
       throw new RuntimeException("PictureContentProvider.update not supported");
    }

    @Override
    public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
       return mimeTypes;
    }

    private File generatePictureFile(Uri uri) throws FileNotFoundException {
       if (generatedUri.compareTo(uri)==0)
          return new File(getContext().getFilesDir(), "picture.png");;
          Context context = getContext();
          String query = uri.getQuery();
          String[] queryParts = query.split("&");
          String pictureCode = "016OA";
          int resolution = 36;
          int frame = 0;
          int padding = 0;
          for (String param : queryParts) {
             if (param.length() < 2)
                continue;
             if (param.substring(0,2).compareToIgnoreCase("p=") == 0) {             
                pictureCode = param.substring(2);
             } else if (param.substring(0,2).compareToIgnoreCase("r=") == 0) {
                resolution = Integer.parseInt(param.substring(2));              
             } else if (param.substring(0, 2).compareToIgnoreCase("f=") == 0) {
                frame = Integer.parseInt(param.substring(2));
             } else if (param.substring(0, 2).compareToIgnoreCase("a=") == 0) {
                padding = Integer.parseInt(param.substring(2));
             }
          }
          Bitmap picture = RenderPictureCode(pictureCode, resolution, frame, padding);
          File tempFile = new File(context.getFilesDir(), "picture.png");       
          FileOutputStream stream;
          stream = new FileOutputStream(tempFile);
          picture.compress(CompressFormat.PNG, 90, stream);
          try {
             stream.flush();
             stream.close();
          } catch (IOException e) {
             e.printStackTrace();
             throw new Error(e);
          }
          picture.recycle();
          generatedUri = uri;
          return tempFile;
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
       File tempFile = generatePictureFile(uri);
       return ParcelFileDescriptor.open(tempFile, ParcelFileDescriptor.MODE_READ_ONLY);
    }
...
}
    <provider 
        android:name="PictureContentProvider"
        android:authorities="com.enigmadream.picturecode.snapshot"
        android:grantUriPermissions="true"
        android:readPermission="com.enigmadream.picturecode.snapshot"
        tools:ignore="ExportedContentProvider">
        <grant-uri-permission android:path="/picture.png" />
    </provider>

创建意图的代码如下所示:

        resolution = mPicView.getWidth();
        if (mPicView.getHeight() > resolution)
            resolution = mPicView.getHeight();
        String paddingText = mPadding.getEditableText().toString();
        int padding;
        try {
            padding = Integer.parseInt(paddingText);
        } catch (NumberFormatException ex) {
            padding = 0;
        }
        Uri uri = Uri.parse(PictureContentProvider.CONTENT_URI 
            + "?p=" + Uri.encode(mPicView.getPictureCode()) + "&r=" + Integer.toString(resolution) 
            + "&f=" + Integer.toString(mPicView.getFrame()) + "&a=" + Integer.toString(padding));
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/png");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        share.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_subject_made));
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(share, getString(R.id.menu_share)));

当错误发生在我的手机上时,这里是堆栈跟踪的前两行:

04-07 13:56:24.423:E/DatabaseUTILS(19431):java.lang.SecurityException:权限拒绝:读取com.enigmadream.pictureCode.pictureContentProvider uri内容://com.enigmadream.pictureCode.snapshot/picture.png?p=01v131&r=36&f=0&a=0来自pid=19025,uid=10062需要com.enigmadream.pictureCode.snapshot

共有1个答案

公西承
2023-03-14

我不熟悉我需要或不需要的权限,也不熟悉如何禁用它们

尝试替换:

<provider 
    android:name="PictureContentProvider"
    android:authorities="com.enigmadream.picturecode.snapshot"
    android:grantUriPermissions="true"
    android:readPermission="com.enigmadream.picturecode.snapshot"
    tools:ignore="ExportedContentProvider">
    <grant-uri-permission android:path="/picture.png" />
</provider>

与:

<provider 
    android:name="PictureContentProvider"
    android:authorities="com.enigmadream.picturecode.snapshot"
    android:exported="true"
    tools:ignore="ExportedContentProvider">
</provider>
 类似资料:
  • 我有一个图像,我想创建一个按钮点击一样的效果-当按钮被触摸时,它应该改变颜色一个瞬间。

  • 当我们共享这样的链接时,我们如何在我们的网站中包含一个图像来显示在WhatsApp中?

  • 假设你已经通过开放图协议的meta标签为你的网页设置了facebook图像,如下所示:

  • 问题内容: 仅使用CSS,是否可以使元素的元素半透明,但元素的内容(文本和图像)不透明? 我想在没有文本和背景作为两个独立元素的情况下完成此操作。 尝试时: 子元素似乎受到其父对象的不透明性的影响,因此相对于父元素的父对象而言。 问题答案: 使用半透明的PNG图片或使用CSS 3:

  • 我正在尝试使用python发送电子邮件(Gmail),但我得到以下错误。 Python脚本如下所示。

  • 我提供了一个Spring statemachine,其中包含库中的基本程序状态,作为不同实现的基础。使用基本statemachine根据当前实现添加自定义转换操作似乎很容易,因为提供了转换注释。 我现在的问题是: 我是否遗漏了文档中的某些内容,是否有一种内置的方法可以做到这一点? 为库中提供的基本statemachine获得实现依赖项保护的其他解决方案吗?