在Twitter和电子邮件上共享文本和图像不会在android 4.4 ACTION_SEND中加载图像

时间:2014-02-08 18:19:56

标签: android android-intent twitter android-sharing

我已经在Twitter上制作了分享文字和图片的意图为ACTION_SEND,我的图片是从我的资产文件夹访问的,为了获取访问这些资源的权限已经实现了内容提供商并分配了权限,我测试了在Android 4.1.2中共享并运行,但是当用android 4.4测试时标记我一个错误并且不过滤你不能添加图像,文本我只是添加,我做了一个测试更改该图像为drawable并列出图像为一个出版商,但不再通过添加。

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已经改变了访问内容提供商资源的方式

1 个答案:

答案 0 :(得分:1)

是的,Android 4.4 KitKat确实改变了很多东西。由于您使用MmsSms提供程序发送消息,它将(静默)拒绝写入ContentProvider,除非您是“默认SMS应用程序”

https://developer.android.com/about/versions/android-4.4.html#SMS

我们公司正在制定一个“开放式”解决方案,但我现在还不知道......它根本不适合你。