2012-07-31 23 views
5

我想在我的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); 
} 

}

+0

你能弄清楚這一點嗎?我還沒有看到它爲Facebook的工作。 – selsine 2013-04-05 15:55:17

回答

1

謝謝你,你的問題解決了我的;) 我有你的正好相反的問題:每個服務將除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); 
} 

這適用於我已經安裝了除G +的ACTION_SEND意圖每一個服務。 使用您的查詢方法也可以用於Google plus。

+0

可以請你幫我,我無法附加使用fb,藍牙,微博等內容提供商的圖像 – user3233280 2014-06-06 18:37:14

相關問題