[英]Android: Reliably share an image from assets via messaging, G+, twitter, facebook?
我正在嘗試在我的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);
}
}
謝謝,你的問題解決了我的問題;)我遇到了與你完全相反的問題:每個服務都可以工作,除了g +。 我在查詢方法中返回null,這使得g +崩潰。
實際暴露我的圖像的唯一方法是實現openFile()
。 我將我的圖像存儲在文件系統上,而不是存儲在資源中,但我想您可以從AssetFileDescriptor
獲取ParcelFileDescriptor
並將其返回。 我的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);
}
這適用於我使用ACTION_SEND
意圖安裝的每個服務,除了g +。 使用您的查詢方法也可以使用google plus。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.