从 ACTION_PICK 共享文件 uri
Share file uri from ACTION_PICK
我正在尝试制作一个应用程序,要求用户选择一个图像文件,然后通过 intent 将其发送到另一个应用程序(在这种情况下是 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();
}
这个 returns 一个 "content://" URI,但是当通过 intent 发送它时:
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 app" uri,是否与权限有关?
这样做的正确方法是什么?我是否需要复制文件以便我的应用程序能够共享它?
编辑:
这适用于 Gmail 等其他应用程序,但不适用于 whatsapp,因此是否有适用于所有或大多数应用程序的解决方案?我不在乎我是否必须制作单独的共享方法,但 whatsapp 是必须的。
代码在真实设备上运行良好,在 android 模拟器上却无法运行。
没想到,为了以防万一phone我试过了。
此代码片段在模拟器和真实设备中都能正常工作
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
}
正如您的代码日志所述,您无法将 uri 转换为字符串数组列表。
所以尝试 将 StringURI 转换为 ArrayListURI 像这样
ArrayList<Uri> tempURI = new ArrayList<>();
tempURI.add(imageUri); //Your String URI
现在有意通过 tempURI
。
sendIntent.putExtra(Intent.EXTRA_STREAM, tempURI);
试试这个并检查它是否在模拟器上工作..
另请阅读此文
ACTION_SEND
支持EXTRA_STREAM
,但只支持单Uri
。 ACTION_SEND_MULTIPLE
支持 EXTRA_STREAM
和 ArrayList<Uri>
。
我正在尝试制作一个应用程序,要求用户选择一个图像文件,然后通过 intent 将其发送到另一个应用程序(在这种情况下是 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();
}
这个 returns 一个 "content://" URI,但是当通过 intent 发送它时:
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 app" uri,是否与权限有关?
这样做的正确方法是什么?我是否需要复制文件以便我的应用程序能够共享它?
编辑:
这适用于 Gmail 等其他应用程序,但不适用于 whatsapp,因此是否有适用于所有或大多数应用程序的解决方案?我不在乎我是否必须制作单独的共享方法,但 whatsapp 是必须的。
代码在真实设备上运行良好,在 android 模拟器上却无法运行。
没想到,为了以防万一phone我试过了。
此代码片段在模拟器和真实设备中都能正常工作
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
}
正如您的代码日志所述,您无法将 uri 转换为字符串数组列表。
所以尝试 将 StringURI 转换为 ArrayListURI 像这样
ArrayList<Uri> tempURI = new ArrayList<>();
tempURI.add(imageUri); //Your String URI
现在有意通过 tempURI
。
sendIntent.putExtra(Intent.EXTRA_STREAM, tempURI);
试试这个并检查它是否在模拟器上工作..
另请阅读此文
ACTION_SEND
支持EXTRA_STREAM
,但只支持单Uri
。 ACTION_SEND_MULTIPLE
支持 EXTRA_STREAM
和 ArrayList<Uri>
。