我用意图ACTION_SEND在twitter上做了分享文字和图片,我的图片是从我的assets文件夹中访问的,对于访问这些资源的权限已经实现了一个内容提供者并分配了权限,我在android中测试了分享的动作4.1.2 并正在运行,但是在使用 android 4.4 进行测试时标记我一个错误并且不过滤您无法添加图像,给我发短信添加,我做了一个测试,更改了可绘制的图像并将图像列为发布者但是不再通过添加来完成。
manifest.xml 中我的内容提供者的声明
<provider
android:name="com.domain.app.helper.ImageProvider"
android:authorities="com.domain.app"
android:grantUriPermissions="true"
android:exported="true"
android:multiprocess="true"
>
</provider>
我的内容提供者类
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
public class ImageProvider extends ContentProvider {
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
return false;
}
@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
String arg4) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
AssetManager am = getContext().getAssets();
String file_name = uri.getLastPathSegment();
Log.i("ICP", file_name);
if(file_name == null)
throw new FileNotFoundException();
AssetFileDescriptor afd = null;
try {
afd = am.openFd("images/recipes/" + file_name);
} catch (IOException e) {
e.printStackTrace();
}
return afd;
}
}
在共享方面使用我的内容提供者
@SuppressLint("DefaultLocale")
private void share(View v, String nameApp, String infoText, String nameImage) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
if (info.activityInfo.packageName.toLowerCase().contains(nameApp) ||
info.activityInfo.name.toLowerCase().contains(nameApp)) {
//fix Facebook only share link
if (info.activityInfo.packageName.toLowerCase().contains("facebook") ||
info.activityInfo.name.toLowerCase().contains("facebook")){
targetedShare.setType("text/plain"); // put here your mime type
targetedShare.putExtra(android.content.Intent.EXTRA_SUBJECT, "The app");
targetedShare.putExtra(android.content.Intent.EXTRA_TEXT, context.getResources().getString(R.string.url_short_app) );
}
else{
///targetedShare.setType( "image/*");
targetedShare.setType( "*/*");
String bodyMsg = infoText + ", " + context.getResources().getString(R.string.firm_shared) +" > " + context.getResources().getString(R.string.url_short_app);
targetedShare.putExtra(Intent.EXTRA_TEXT, bodyMsg);
//this change for test
//Uri uri = Uri.parse("android.resource://"+context.getPackageName()+"/drawable/table");
Uri theUri = Uri.parse("content://"+context.getPackageName()+"/"+nameImage);
targetedShare.putExtra(Intent.EXTRA_STREAM, theUri);
}
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
v.getContext().startActivity(chooserIntent);
}
} catch (Exception e) {
Log.i("ERROR_SHARE", e.toString());
}
}
我希望你能帮我找出问题所在
我假设 android 4.4 已经改变了访问内容提供者资源的方式