Android development learning using intent to realize WeChat sharing

introduce

The sharing function is a function that almost every social application must have. Its implementation can either call the relevant third-party sdk, or use the intent to directly realize the sharing. However, the intention method, my Android7.0 mobile phone only supports forwarding local pictures to the circle of friends


accomplish

ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
Intent intent = new Intent(Intent.ACTION_SEND); // 设置intent的action
intent.setComponent(componentName); // Set the target activity name of the intent is preceded by the package name, followed by the full name of the activity
intent.setType("image/*"); // set the type of intent
String path = Environment.getExternalStorageDirectory().getPath() + "/Pictures/dongqiudi/1523624189281.jpg";
File file = new File(path);
if (file.exists()) {
   Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.songzeceng.myFileProvider", file); // Get the local file uri
   intent.putExtra(Intent.EXTRA_STREAM, uri); // Store the uri in the intent, the key name is a fixed value
}
intent.putExtra("Kdescrpition","Test forwarding a picture"); // Store the description text, the key name is also a fixed value
startActivity(intent); // Send the intent and enter the WeChat Moments sharing interface

The code is simple and the steps are clear and straightforward. It's just that in the high version of Android, FileProvider must be used to obtain the local file uri. For the usage of FileProvider, please refer to my article on the use of FileProvider for Android development and learning


operation result

directly above



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324918774&siteId=291194637