1

I am trying to get the StreamProvider to share images in my assets/stamps folder, but am doing something wrong. Sorry for asking such a trivial question. Using the demo as an example this is where I am at.

manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.np161.stamp2" >

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.GET_TOP_ACTIVITY_INFO" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
    android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="Stickers 2"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    <activity
        android:name=".MainActivity"
        android:label="Stickers 2"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
    <activity
        android:name=".chatHeadPreviewActivity"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Transparent" />
    <activity
        android:name=".HomeScreen"
        android:launchMode="singleInstance"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="Stickers 2"
        android:screenOrientation="portrait"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".ChatHeadService" />

    <activity
        android:name=".newActivity"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".Settings"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".Language"
        android:screenOrientation="portrait"
        android:label="@string/title_activity_language" >
    </activity>
</application>

<provider
    android:name="com.commonsware.cwac.provider.StreamProvider"
    android:authorities="com.example.np161.stamp2"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="com.commonsware.cwac.provider.STREAM_PROVIDER_PATHS"
        android:resource="@xml/paths"/>
</provider>


</manifest>

my paths.xml contains this

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <asset name="assets" path="stamps" />
</paths>

The activity where I am sending the image

assetManager = getResources().getAssets();

    try {
        images = getAssets().list("stamps");
        listImages = new ArrayList<String>(Arrays.asList(images));
    }catch(IOException e){

    }

I use this to populate my gridview and then set the onclick method

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            String path = "assets/" + listImages.get(position);

            Intent shareIntent = new Intent(Intent.ACTION_SEND);

            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            shareIntent.putExtra(Intent.EXTRA_STREAM, getUri(path));

            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            shareIntent.setType("image/png");

            shareIntent.setPackage(ChatHeadService.app);

            startActivity(Intent.createChooser(shareIntent, "Share with"));

modified getUri method

private static final Uri PROVIDER= Uri.parse("content://com.example.np161.stamp2");

private Uri getUri(String pathEnd) {
    String path=pathEnd;

    return(PROVIDER.buildUpon().path(path).build());
}

The error i seem to be getting is

02-24 10:01:53.280 17563-17610/? E/JHEAD: can't open '/image/png'
02-24 10:01:53.283 17563-17610/? E/JHEAD: can't open '/image/png'
02-24 10:01:53.326 17563-17610/? E/ImageFileManager: Image resize fail
02-24 10:01:53.422 17563-17609/? E/SQLiteLog: (1555) abort at 13 in [INSERT INTO sticker(package_id,sticker_id,order_num,image_width,image_height) VALUES (?,?,?,?,?)]: UNIQUE constraint failed: sticker.sticker_id

Thanks for taking the time to look at this. I am still relatively new to android programming

user3074140
  • 733
  • 3
  • 13
  • 30
  • What is the actual `Uri` that you wound up using, that generated those error messages? It should be something of the form `content://com.example.np161.stamp2/assets/...`, where the `...` part is a relative path into your `assets/` directory of your `app/` module or project. – CommonsWare Feb 24 '16 at 01:04
  • @CommonsWare the uri is D/new URI: content://com.example.np161.stamp2/assets/stp_003.png – user3074140 Feb 24 '16 at 01:11

1 Answers1

2

Assuming that you have something like app/src/main/assets/stamps/stp_003.png in your project, that looks fine.

More importantly, it suggests that "can't open '/image/png'" must be pulling from the MIME type. However, the leading slash on that seems like the other app is treating the MIME type as a path, which makes no sense.

You might try sharing with a couple of other apps, to see if they accept your Uri, but from the first three lines of that error, it would seem like the problem is on their side, and not in any way that you or I are likely to be able to fix.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • ah the stp_003.png is in a subfolder in the assets folder called stamps. – user3074140 Feb 24 '16 at 01:16
  • @user3074140: No, sorry, that's my fault. I forgot that path segment when writing the answer. – CommonsWare Feb 24 '16 at 01:16
  • It seems to work fine with things like facebook messenger and twitter, so its just the Line app which is a problem, unfortunately thats the most important app to share with, but either thank you for the help. – user3074140 Feb 24 '16 at 01:24
  • @user3074140: You may have to go with having the stamps at least temporarily out on external storage and using a `file://` `Uri`. Not all apps are up to snuff with respect to `content://` `Uri` values, particularly if they are not a typical destination for shared content. – CommonsWare Feb 24 '16 at 01:27
  • @CommonWare Yes I have tried that but have a strange issue that from my device it shows me sending different images but the receiver just receives the first image sent over and over again. – user3074140 Feb 24 '16 at 01:33