Android: securely share image from assets via messaging, g +, twitter, facebook?

I am trying to create a button in my Android application, which allows the user to share the image using my choice of social networking networks. The image file is stored in the application resource folder.

My plan is to implement a custom ContentProvider to provide external access to the image, and then send the TYPE_SEND intent, which defines the image uri in my content provider.

I did this and it works for Google+ and GMail, but for other services it fails. The hardest part is finding information about what I should return from the query () method of my ContentProvider. Some applications specify a projection (for example, Google+ asks for _id and _data), while some applications skip zero as a projection. Even where the projection is indicated, I do not know what actual data (types) are expected in the columns. I can not find documentation on this.

I also implemented the openAssetFile ContentProvider method, and this is called (twice by Google+!), But then the request method is inevitably called. It seems that only the result of the query method is calculated.

Any ideas I'm wrong about? What should I return from my query method?

Code below:

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

}

+5
source
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); 
}

, ACTION_SEND, g +. google plus.

+1

All Articles