0

I tried attaching a image from asset folder to email. But I haven't got considerable success in that while I have been able to tweet an image from asset with help of ContentProvider

public class AssetProvider extends 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;
}

}

Code for posting image from asset folder to 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;
                }
            }

But while doing same for email it gives a message "Can't attach an empty file"

Email:

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);

Please suggest.

Thanks in advance.

frogEye
  • 364
  • 3
  • 14

1 Answers1

0

The Latest Facebook App versions doesn't allow you to share text using intents. You have to use the Facebook SDK to share/publish a post or you can share only images or url.

Please refer this post, it says Facebook Design Team has intentionally closed pre-filling a message as it is against their policy.

For more information you can refer these Stack Overflow Questions :

  1. Share Text on Facebook from Android App via ACTION_SEND
  2. Android: How to share image with text on facebook via intent?

Edit 1 : There is a work around refer this SO Answer.

Community
  • 1
  • 1
Vipul Asri
  • 8,903
  • 3
  • 46
  • 71