Here I will share with you some of the knowledge I have summarized on the Internet, I hope it will be helpful to everyone
Register ACTION event in AndroidManifest.xml
<activity
android:name="com.test.app.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="这里的名称会对外显示"
android:launchMode="singleTask"
android:screenOrientation="portrait">
//注册接收分享
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
//接收分享的文件类型
<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>
//注册默认打开事件,微信、QQ的其他应用打开
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
//接收打开的文件类型
<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>
Add receiving code in the Activity used to receive sharing
- When the APP process is in the background, the onNewIntent method of the Activity will be called
- When the APP process is killed, the onCreate method will be called
So you need to listen to 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);
}
The receiveActionSend method is as follows
public void receiveActionSend(Intent intent) {
String action = intent.getAction();
String type = intent.getType();
//判断action事件
if (type == null || (!Intent.ACTION_VIEW.equals(action) && !Intent.ACTION_SEND.equals(action))) {
return;
}
//取出文件uri
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;
}
//业务处理
.
.
.
}
Get the real path getFileFromUri method
/**
* 获取真实路径
*
* @param context
*/
public static String getFileFromUri(Context context, Uri uri) {
if (uri == null) {
return null;
}
switch (uri.getScheme()) {
case ContentResolver.SCHEME_CONTENT:
//Android7.0之后的uri content:// URI
return getFilePathFromContentUri(context, uri);
case ContentResolver.SCHEME_FILE:
default:
//Android7.0之前的uri file://
return new File(uri.getPath()).getAbsolutePath();
}
}
The uri content:// URI after Android 7.0 needs to be compatible with third-party apps such as WeChat and QQ
- When the file management is selected to open this application, the value of the url is content://media/external/file/85139
- When selecting this app to open in WeChat, the url value is content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc
- When you choose to open this application in QQ, the url value is content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/
The first is the unified file resource of the system, which can be converted into an absolute path through the system method;
WeChat and QQ are fileProvider, which can only obtain the file stream, and you need to copy the file to your own private directory first.
Methods as below:
/**
* 从uri获取path
*
* @param uri content://media/external/file/109009
* <p>
* FileProvider适配
* 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();
}
return data;
}
/**
* 用流拷贝文件一份到自己APP私有目录下
*
* @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) {
int read;
byte[] buffer = new byte[8 * 1024];
//自己定义拷贝文件路径
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;
}