通过意图共享图像时权限被拒绝

时间:2014-06-01 16:08:26

标签: android android-intent android-fileprovider

我正在尝试使用Intent在应用的.apk(在res / drawable中)共享图像。在developer.android.com文档([1][2][3])之后,我认为我需要执行以下操作:

  1. 将res / drawable中的图片作为Bitmap获取,并将其保存在应用的内部存储空间中,
  2. 使用FileProvider,获取图片的内容uri
  3. 使用Intent操作创建ACTION_SEND并将uri添加为流
  4. Intent.FLAG_GRANT_READ_URI_PERMISSION标志添加到意图
  5. 在实践中,我刚刚创建了默认的“Hello World”应用程序,用触发图像共享的按钮替换了主活动中的文本视图,并尝试从res / drawable共享标准的ic_launcher.png图像。 / p>

    一切运行正常,直到我选择其他应用程序与选择器对话框共享图像。无论我选择什么(Adobe Reader,Facebook,MMS),接收图像的应用程序都会崩溃并在其日志中报告以下错误:

    06-01 02:06:53.863: W/ActivityManager(356): Permission denied: checkComponentPermission() owningUid=10099
    06-01 02:06:53.953: E/AndroidRuntime(21961): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adobe.reader/com.adobe.reader.services.cpdf.ARCreatePDFActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    

    使用Android调试监视器我可以看到图像文件是在应用程序的内部存储中创建的,对所有者具有rw权限,对其他人没有权限。

    更新 我终于有时间在手机(4.3)和平板电脑(4.2)上进行更彻底的测试。在两台设备上,我得到了完全相同的结果。 “权限被拒绝”警告始终显示在日志中,但最终结果可能因应用程序而异:

    • 标准Android电子邮件应用 - 发送电子邮件时没有错误/崩溃但是 包含附件
    • Gmail应用 - 电子邮件将一起发送 附件(这是我找到上述代码的唯一情况 工作),
    • Facebook,Twitter,Adobe Reader - 这些都崩溃了 选择器对话框中的选择时刻;

    这是否意味着没有“标准”方式在应用内部存储中共享(图像)资源,并且需要根据接收应用自定义共享逻辑?

    的AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.imagesharetest.share" >
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name="com.imagesharetest.share.SharingActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="com.imagesharetest.share.fileprovider"
                android:grantUriPermissions="true"
                android:exported="false">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/filepaths" />
            </provider>
        </application>
    </manifest>
    

    RES / filepaths.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <files-path
            name="shareImages"
            path="images/" />
    </paths>
    

    活动xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${packageName}.${activityClass}">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go!"
            android:id="@+id/shareButton"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:onClick="shareImage"/>
    </RelativeLayout>
    

    活动.java:

    package com.imagesharetest.share;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v4.content.FileProvider;
    import android.view.View;
    
    import java.io.File;
    import java.io.FileOutputStream;
    
    public class SharingActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sharing);
        }
    
        public void shareImage(View v) {
            Uri icLauncherFileURI = FileProvider.getUriForFile(
                    SharingActivity.this, "com.imagesharetest.share.fileprovider",
                    getICLauncherBitmapAsInternalFile());
    
            Intent shareIntent = getShareIntentFor(icLauncherFileURI);
            startActivity(Intent.createChooser(shareIntent, "Sending ic_launcher..."));
        }
    
        private File getICLauncherBitmapAsInternalFile() {
            Bitmap icLauncherBitmap = BitmapFactory.decodeResource(
                    getResources(), R.drawable.ic_launcher);
            File imagePath = new File(getFilesDir(), "images");
            imagePath.mkdirs();
            File icLauncherFile = new File(imagePath, "shared_image.png");
            saveBitmapAsFile(icLauncherBitmap, icLauncherFile);
    
            return icLauncherFile;
        }
    
        private void saveBitmapAsFile(Bitmap bmp, File file) {
            try {
                FileOutputStream fos = new FileOutputStream(file);
    //            FileOutputStream fos = SharingActivity.this.openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.flush();
                fos.close();
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
    
        private Intent getShareIntentFor(Uri contentUri) {
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
            shareIntent.setType("image/png");
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            return shareIntent;
        }
    }
    

0 个答案:

没有答案