我试图制作一个会要求用户选择图像文件,然后通过意图将其发送到另一个应用程序的应用程序(在这种情况下,Whatsapp可以与其他应用程序一起使用)
我这样做是为了请求文件:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
这将返回一个“ content://” URI,但通过意图发送时:
if (isPackageExisted(whatsAppPackage)) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Some text");
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.setType("image/*");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setPackage(whatsAppPackage);
startActivity(sendIntent);
}else{
goToGooglePlay(whatsAppPackage);
}
Whatsapp举杯敬酒,说该文件格式不受支持,我可以在logcat上看到堆栈跟踪
java.lang.ClassCastException: android.net.Uri$StringUri cannot be cast to java.util.ArrayList
at android.os.Bundle.getParcelableArrayList(Bundle.java:838)
at android.content.Intent.getParcelableArrayListExtra(Intent.java:5405)
at com.whatsapp.ContactPicker.c(ContactPicker.java:12)
at com.whatsapp.ContactPicker.onCreate(ContactPicker.java:526)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
...
我不确定是否可以与其他应用共享“ picker应用” uri,这与权限有关吗?
这样做的正确方法是什么?我需要复制文件以便我的应用程序可以共享它吗?
编辑:
这可以与其他应用程序(例如Gmail)一起使用,但不能在whatsapp中使用,因此可能有一种适用于所有或大多数应用程序的解决方案吗?我不在乎是否必须使用单独的共享方法,但必须使用whatsapp。
答案 0 :(得分:4)
该代码在真实设备上运行良好,在android模拟器上无法正常工作。
我没想到,我曾在电话中尝试过以防万一。
答案 1 :(得分:1)
此代码段代码可在模拟器和真实设备中正常工作
private val FILE_SELECT_CODE = 101
private val whatsAppPackage = "com.whatsapp"
fun sharePicture() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
intent.addCategory(Intent.CATEGORY_OPENABLE)
if (intent.resolveActivity(packageManager) != null){
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE)
}else{
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int,
data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
FILE_SELECT_CODE -> {
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, "Some text")
sendIntent.putExtra(Intent.EXTRA_STREAM, data!!.data)
sendIntent.type = "image/*"
sendIntent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION)
sendIntent.`package` = whatsAppPackage
if (sendIntent.resolveActivity(packageManager) != null) {
startActivity(sendIntent)
} else {
goToGooglePlay(whatsAppPackage)
}
}
}
}
}
fun goToGooglePlay(appPackageName: String) {
val playIntent = Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=$appPackageName"))
if (playIntent.resolveActivity(packageManager) != null){
startActivity(playIntent)
}else
startActivity(Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details? id=$appPackageName")))
}
fun isPackageExisted(packageName: String): Boolean {
val pm = packageManager
val isInstalled: Boolean
isInstalled = try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
return isInstalled
}
答案 2 :(得分:1)
代码日志显示您无法将uri转换为字符串数组列表。
所以尝试将StringURI转换为ArrayListURI ,
ArrayList<Uri> tempURI = new ArrayList<>();
tempURI.add(imageUri); //Your String URI
然后故意传递tempURI
。
sendIntent.putExtra(Intent.EXTRA_STREAM, tempURI);
尝试一下,检查它是否在Simulator上运行。
也请阅读
ACTION_SEND
支持EXTRA_STREAM
,但仅适用于单 Uri
。 ACTION_SEND_MULTIPLE
支持EXTRA_STREAM
和ArrayList<Uri>
。