Android:通过短信,G +,Twitter,Facebook可靠地分享资产中的图像?

fel*_*lix 5 android share android-intent

我正在尝试在我的Android应用程序中创建一个按钮,允许用户使用他们选择的社交媒体网络共享图像.图像文件存储在应用程序的assets文件夹中.

我的计划是实现自定义ContentProvider以提供对图像的外部访问,然后发送TYPE_SEND意图,指定我的内容提供者中的图像的uri.

我这样做了,它适用于Google+和GMail,但对于其他服务,它失败了.最困难的部分是找到我应该从ContentProvider的query()方法返回的信息.某些应用会指定投影(例如,Google +要求_id和_data),而某些应用会将null作为投影传递.即使指定了投影,我也不知道列中预期的实际数据(类型).我找不到这方面的文件.

我还实现了ContentProvider的openAssetFile方法,并且这个方法被调用(两次由Google+!),但随后也不可避免地会调用查询方法.只有查询方法的结果才算数.

我出错的任何想法?我应该从查询方法返回什么?

代码如下:

// my intent

Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("image/jpeg");
Uri uri = Uri.parse("content://com.me.provider/ic_launcher.jpg");
i.putExtra(Intent.EXTRA_STREAM, uri);       
i.putExtra(android.content.Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(i, "Share via"));

// my custom content provider

public class ImageProvider extends ContentProvider
{
private AssetManager _assetManager;

public static final Uri CONTENT_URI = Uri.parse("content://com.me.provider");

// not called
@Override
public int delete(Uri arg0, String arg1, String[] arg2) 
{
    return 0;
}

// not called
@Override
public String getType(Uri uri) 
{
    return "image/jpeg";
}

// not called
@Override
public Uri insert(Uri uri, ContentValues values) 
{
    return null;
}

@Override
public boolean onCreate() 
{
    _assetManager = getContext().getAssets();
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 
{
    MatrixCursor c = new MatrixCursor(new String[] { "_id", "_data" });

    try
    {
                    // just a guess!! works for g+ :/
        c.addRow(new Object[] { "ic_launcher.jpg",  _assetManager.openFd("ic_launcher.jpg") });
    } catch (IOException e)
    {
        e.printStackTrace();
        return null;
    }

    return c;
}

// not called
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) 
{
    return 0;
}

// not called  
@Override
public String[] getStreamTypes(Uri uri, String mimeTypeFilter)
{

    return new String[] { "image/jpeg" };
}

// called by most apps
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException 
{

    try 
    {
        AssetFileDescriptor afd = _assetManager.openFd("ic_launcher.jpg");
        return afd;
    } catch (IOException e) 
    {
        throw new FileNotFoundException("No asset found: " + uri);
    }
}

// not called
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException
{

    return super.openFile(uri, mode);
}
Run Code Online (Sandbox Code Playgroud)

}

小智 1

谢谢,你的问题解决了我的问题;)我遇到了与你完全相反的问题:除了 g+ 之外,每个服务都可以工作。我在查询方法中返回 null,这导致 g+ 崩溃。

真正暴露我的图像的唯一方法是实现openFile(). ParcelFileDescriptor我将图像存储在文件系统中,而不是资产中,但我想您可以从您的中获取AssetFileDescriptor并返回它。我的openFile()方法如下所示:

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException {
    String path = uri.getLastPathSegment();
    if (path == null) {
        throw new IllegalArgumentException("Not a Path");
    }
    File f = new File(getContext().getFilesDir() + File.separator + "solved" + path + ".jpg");
    int iMode;
    if ("r".equals(mode)) {
        iMode = ParcelFileDescriptor.MODE_READ_ONLY;
    } else if ("rw".equals(mode)) {
        iMode = ParcelFileDescriptor.MODE_READ_WRITE;
    } else if ("rwt".equals(mode)) {
        iMode = ParcelFileDescriptor.MODE_READ_WRITE | ParcelFileDescriptor.MODE_TRUNCATE;
    } else {
        throw new IllegalArgumentException("Invalid mode");
    }       
    return ParcelFileDescriptor.open(f, iMode); 
}
Run Code Online (Sandbox Code Playgroud)

这适用于我安装的所有服务(ACTION_SENDg+ 除外)。使用您的查询方法也使其适用于 google plus。