我正在尝试使用Intent
在应用的.apk(在res / drawable中)共享图像。在developer.android.com文档([1],[2],[3])之后,我认为我需要执行以下操作:
Bitmap
获取,并将其保存在应用的内部存储空间中,FileProvider
,获取图片的内容uri Intent
操作创建ACTION_SEND
并将uri添加为流Intent.FLAG_GRANT_READ_URI_PERMISSION
标志添加到意图在实践中,我刚刚创建了默认的“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)上进行更彻底的测试。在两台设备上,我得到了完全相同的结果。 “权限被拒绝”警告始终显示在日志中,但最终结果可能因应用程序而异:
这是否意味着没有“标准”方式在应用内部存储中共享(图像)资源,并且需要根据接收应用自定义共享逻辑?
的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;
}
}