Register ACTION events in androidmanifest.xml

<activity
        android:name="com.test.app.MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="The name here is going to show up."
        android:launchMode="singleTask"
        android:screenOrientation="portrait"<intent-filter> <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT"/> // The file type to be shared <data Android :mimeType="image/*" />
            <data android:mimeType="application/msword" />
            <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
            <data android:mimeType="application/vnd.ms-excel" />
            <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
            <data android:mimeType="application/vnd.ms-powerpoint" />
            <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
            <data android:mimeType="application/pdf" />
            <data android:mimeType="text/plain"<intent-filter> <action android:name= intent-filter"android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT"/> // Receive open file type <data Android :scheme="file" />
            <data android:scheme="content" />
            <data android:mimeType="image/*" />
            <data android:mimeType="application/msword" />
            <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
            <data android:mimeType="application/vnd.ms-excel" />
            <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
            <data android:mimeType="application/vnd.ms-powerpoint" />
            <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
            <data android:mimeType="application/pdf" />
            <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
Copy the code

Add receive code to the Activity that receives shares

  1. When the APP process is in the background, the Activity’s onNewIntent method is called
  2. The onCreate method is called when the APP process is killed

So you need to listen for events in both methods

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        receiveActionSend(intent);
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        receiveActionSend(intent);
    }
Copy the code

The receiveActionSend method is as follows

    public void receiveActionSend(Intent intent) {
        String action = intent.getAction();
        String type= intent.getType(); // Determine the action eventif (type== null || (! Intent.ACTION_VIEW.equals(action) && ! Intent.ACTION_SEND.equals(action))) {return; Uri uri = intent.getData();if(uri == null) { uri = intent.getParcelableExtra(Intent.EXTRA_STREAM); String filePath = uriutils.getFileFromuri (edusohoapp.baseapp, uri);if (TextUtils.isEmpty(filePath)) {
            return; } // Business processing..}Copy the code

GetFileFromUri method to get the real path

** @param context */ public static String getFileFromUri(context context, Uri Uri) {if (uri == null) {
            return null;
        }
        switch (uri.getScheme()) {
            caseContentresolver.scheme_content :// Android7.0 uri content:// URIreturn getFilePathFromContentUri(context, uri);
            caseContentresolver.scheme_file: default: // the uri file:// before Android7.0returnnew File(uri.getPath()).getAbsolutePath(); }}Copy the code

Android7.0 uri content:// Uri needs to be compatible with wechat, QQ and other third-party apps

  • In file management to open the application, the value of the url for the content: / / media/external/file / 85139
  • Choose this application in WeChat opens, the value of the url for the content: / / com. Tencent. Mm. External. Fileprovider/external/tencent/MicroMsg/Download / 111. The doc
  • Choose this application in QQ is opened, the value of the url for the content: / / com. Tencent. Mobileqq. Fileprovider external_files/storage/emulated / 0 / tencent/QQfile_recv /

The first is the system unified file resource, which can be converted into an absolute path through the system method. Wechat and QQ are FileProviders and can only obtain file streams. You need to copy the files to your own private directory first. The method is as follows:

/ get the path from the uri * * * * * @ param uri for the content: / / media/external/file / 109009 * < p > * * FileProvider adaptation content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/ * content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/ */ private static String getFilePathFromContentUri(Context context, Uri uri) {if (null == uri) return null;
        String data = null;

        String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
        Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
        if(null ! = cursor) {if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                } else {
                    int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
                    String fileName = cursor.getString(nameIndex);
                    data = getPathFromInputStreamUri(context, uri, fileName);
                }
            }
            cursor.close();
        }
        returndata; } /** * stream a copy of the file to your APP private directory ** @param context * @param uri * @param fileName */ private static String getPathFromInputStreamUri(Context context, Uri uri, String fileName) { InputStream inputStream = null; String filePath = null;if(uri.getAuthority() ! = null) { try { inputStream = context.getContentResolver().openInputStream(uri); File file = createTemporalFileFrom(context, inputStream, fileName); filePath = file.getPath(); } catch (Exception e) { } finally { try {if(inputStream ! = null) { inputStream.close(); } } catch (Exception e) { } } }return filePath;
    }

    private static File createTemporalFileFrom(Context context, InputStream inputStream, String fileName)
            throws IOException {
        File targetFile = null;

        if(inputStream ! = null) { intread; byte[] buffer = new byte[8 * 1024]; / / define your own copy File path targetFile = new File (context. GetExternalCacheDir (), fileName);if (targetFile.exists()) {
                targetFile.delete();
            }
            OutputStream outputStream = new FileOutputStream(targetFile);

            while ((read= inputStream.read(buffer)) ! = -1) { outputStream.write(buffer, 0,read); } outputStream.flush(); try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); }}return targetFile;
    }
Copy the code