There's a little documentation and support about how to send and share images (or files) from the assets folder for Android.
Basically, I've been supported by these links:
and many similar.
But anyway, the code I'm using is:
CONTENT PROVIDER
public class AssetsProvider extends ContentProvider {
private static final String LOGTAG = "MD/AssetsProvider";
@Override
public AssetFileDescriptor openAssetFile( Uri uri, String mode ) throws FileNotFoundException
{
Log.v(LOGTAG, "AssetsGetter: Open asset file");
AssetManager am = getContext( ).getAssets( );
String file_name = uri.getPath().substring(1, uri.getPath().length());
//String file_name = uri.getLastPathSegment();
// Neither of the two lines above work for me
if( file_name == null )
throw new FileNotFoundException( );
AssetFileDescriptor afd = null;
try
{
afd = am.openFd( file_name );
}
catch(IOException e)
{
e.printStackTrace( );
}
return afd;//super.openAssetFile(uri, mode);
}
@Override
public String getType( Uri p1 )
{
// TODO: Implement this method
return null;
}
@Override
public int delete( Uri p1, String p2, String[] p3 )
{
// TODO: Implement this method
return 0;
}
@Override
public Cursor query( Uri p1, String[] p2, String p3, String[] p4, String p5 )
{
// TODO: Implement this method
return null;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal )
{
// TODO: Implement this method
return super.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal );
}
@Override
public Uri insert( Uri p1, ContentValues p2 )
{
// TODO: Implement this method
return null;
}
@Override
public boolean onCreate( )
{
// TODO: Implement this method
return false;
}
@Override
public int update( Uri p1, ContentValues p2, String p3, String[] p4 )
{
// TODO: Implement this method
return 0;
}
}
Which I don't like very much because it requires API 16.
ANDROID MANIFEST (inside <application>
tag)
<provider
android:name="package.name.utils.AssetsProvider"
android:authorities="package.name"
android:grantUriPermissions="true"
android:exported="true" />
CODE TO SHARE IMAGE
Uri theUri = Uri.parse("content://package.name/share_picture.png");
Intent theIntent = new Intent(Intent.ACTION_SEND);
theIntent.setType("image/*");
theIntent.putExtra(Intent.EXTRA_STREAM,theUri);
theIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"");
theIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
startActivity(theIntent);
and I can see my chooser successfully.
MY MAIN PROBLEM IS
I can get my picture shared IN SOME APPS but not other apps. For example: Apps where I can share my picture:
Google Hangouts
Skype (it stucks in "sending")
Dropbox
Apps where I can't share my picture:
Inbox by Gmail
Gmail
Any app for image editing
Messages
Slack
List item
Hipchat
Messenger (by Facebook)
is there anything wrong with the apps where I can't share? or am I doing anything wrong on the code? maybe the setType
function?
Thank you in advance.
Regards.
Rafael.
The problem also appears when sharing an image from File, so nothing related with Assets is the problem:
This morning I've done this test:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(folderToSaveFiles+relativeNameSharePicture);
shareIntent.setData(phototUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
startActivity(Intent.createChooser(shareIntent, "Share Via"));
and the same happens!!
The main problem is that the Uri
has to be parsed from a File
with Uri.fromFile(File file)
then everything works ok!