Android Share 将图片或者文件分享到自己的APP 中

https://blog.csdn.net/qq_34983989/article/details/78438254?tdsourcetag=s_pcqq_aiomsg

https://blog.csdn.net/gjy211/article/details/72626233

 

一般我们看到好看的图片或者文章时,就会将其分享到我们的微信朋友,或者朋友圈中,就像这样


这个呢,我们可以调用系统的分享,或者用第三方的SDK,如ShareSDK, 友盟都是可以的,但是,我们想要把文件分享到我们的APP 中呢。

一,我们需要在清单文件中进行一些信息配置,让它们可以检测到我们的APP。
<intent-filter android:label="@string/app_name" >
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="*/*" />
            </intent-filter>

这个是任意的文件,没有指定文件类型。

PDF 文件

         <intent-filter>
                 <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />


                <data android:scheme="file" />
                <data android:mimeType="*/*" />
                <data android:pathPattern=".*\\.pdf" />
                <data android:host="*" />
            </intent-filter>

图片

            <intent-filter>
                 <action android:name="android.intent.action.SEND" />
               <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/jpeg" />
            </intent-filter>
            <intent-filter>
                 <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/png" />
            </intent-filter>
网址

            <intent-filter>
                 <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/html" />
            </intent-filter>

二,接收数据
在起始的activity中做如下处理

        Intent itnIn=getIntent();
        Bundle extras = itnIn.getExtras();
        String action = itnIn.getAction();
        if (Intent.ACTION_SEND.equals(action)) {
            if (extras.containsKey(Intent.EXTRA_STREAM)) {
                try {
                    // Get resource path from intent
                    Uri uri2 = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);

// 返回路径
                    String path = getRealPathFromURI(Activity_Logo_View.this, uri2);
                } catch (Exception e) {
                    Log.e(this.getClass().getName(), e.toString());
                }

/**
     * 通过Uri获取文件在本地存储的真实路径
     * @param act
     * @param contentUri
     * @return
     */
    public String getRealPathFromURI(Activity act, Uri contentUri) {

// can post image
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = act.managedQuery(contentUri, proj, // Which columns to return
                null, // WHERE clause; which rows to return (all rows)
                null, // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        if (cursor==null) {
            String path = contentUri.getPath();
            return path;
        }

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);

    }

返回的path 就是我们需要的文件路径。

相关推荐

网友评论(0)