无法通过资产文件夹将文件附加到android中的电子邮件应用程序

时间:2015-10-15 06:21:30

标签: android twitter email-attachments sharing

我尝试将资产文件夹中的图像附加到电子邮件中。但是,由于我在ContentProvider的帮助下能够从资产中发布图片,因此我没有取得相当大的成功

公共类AssetProvider扩展了ContentProvider {

@Override
public AssetFileDescriptor openAssetFile( Uri uri, String mode ) throws FileNotFoundException
{
    Log.v("HERREEE", "AssetsGetter: Open asset file");
    AssetManager am = getContext( ).getAssets( );
    String file_name = uri.getLastPathSegment( );
    if( file_name == null )
        throw new FileNotFoundException( );
    AssetFileDescriptor afd = null;
    try
    {
        afd = am.openFd( file_name );
    }
    catch(IOException e)
    {
        e.printStackTrace( );
    }
    return afd;//super.openAssetFile(uri, mode);
}

@Override
public String getType( Uri p1 )
{
    // TODO: Implement this method
    return null;
}

@Override
public int delete( Uri p1, String p2, String[] p3 )
{
    // TODO: Implement this method
    return 0;
}

@Override
public Cursor query( Uri p1, String[] p2, String p3, String[] p4, String p5 )
{
    // TODO: Implement this method
    return null;
}

@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal )
{
    // TODO: Implement this method
    return super.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal );
}

@Override
public Uri insert( Uri p1, ContentValues p2 )
{
    // TODO: Implement this method
    return null;
}

@Override
public boolean onCreate( )
{
    return false;
}

@Override
public int update( Uri p1, ContentValues p2, String p3, String[] p4 )
{
    // TODO: Implement this method
    return 0;
}

}

将资产文件夹中的图片发布到Twitter的代码:

Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT,"Test tweet");
            intent.setType("*/*");
            final PackageManager pm = mContext.getPackageManager();
            final List<?> activityList = pm.queryIntentActivities(intent, 0);
            int len =  activityList.size();
            boolean isFound = false;
            for (int i = 0; i < len; i++) {
                final ResolveInfo app = (ResolveInfo) activityList.get(i);
                if(app.activityInfo.packageName.contains("com.twitter.android")){
                    isFound = true;
                    final ActivityInfo activity=app.activityInfo;
                    final ComponentName name=new ComponentName(activity.applicationInfo.packageName, activity.name);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    intent.setComponent(name);
                    Uri uri = Uri.parse("content://com.authority/file:///android_asset/image.png");
                    intent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
                    mContext.startActivity(intent);
                    break;
                }
            }

但是,在为电子邮件执行相同操作时,它会显示“无法附加空文件”的消息

电子邮件:

Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_SUBJECT, "Checkout MY Application");
        intent.putExtra(Intent.EXTRA_TEXT, "Test mail");
        Uri uri = Uri.parse("content://com.authority/file:///android_asset/image.png"));
        intent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
        mContext.startActivity(intent);

请建议。

提前致谢。

1 个答案:

答案 0 :(得分:0)

最新的Facebook应用程序版本不允许您使用意图共享文本。您必须使用Facebook SDK共享/发布帖子,或者您只能共享图片或网址。

请参阅此post,它说Facebook设计团队故意关闭预先填写邮件,因为它违反了他们的政策。

有关更多信息,请参阅这些Stack Overflow问题:

  1. Share Text on Facebook from Android App via ACTION_SEND
  2. Android: How to share image with text on facebook via intent?
  3. 修改1 :有一项解决方法请参阅此SO Answer