我想在我的android應用程序中創建一個按鈕,允許用戶使用他們選擇的社交媒體網絡共享圖像。圖像文件存儲在應用程序的資產文件夾中。Android:通過消息,G +,Twitter,Facebook可靠地共享資產圖片?
我的計劃是實現一個自定義的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);
}
}
你能弄清楚這一點嗎?我還沒有看到它爲Facebook的工作。 – selsine 2013-04-05 15:55:17