gpt4 book ai didi

java - 无法使用 ShareIntent 从 Android 中的可绘制文件夹发送图像文件?

转载 作者:行者123 更新时间:2023-12-01 13:35:05 25 4
gpt4 key购买 nike

我想使用 Intent 共享图像,我使用了 answer of Dixit patel 中的代码.

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
File.separator + "icon_ask_fatwa_one.png");
Uri imageUri = Uri.parse("android.resource://"+getPackageName()+"/"+"drawable/icon_ask_fatwa_one");
Log.i("imageUri",""+imageUri);
share.putExtra(Intent.EXTRA_STREAM,imageUri);
startActivity(Intent.createChooser(share, "Share Image"));
}}

尝试在模拟器中运行此代码时出现以下错误:

01-25 11:50:57.697: E/AndroidRuntime(265): FATAL EXCEPTION: main 01-25 11:50:57.697: E/AndroidRuntime(265): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mms/com.android.mms.ui.ComposeMessageActivity}: java.lang.NullPointerException 01-25 11:50:57.697: E/AndroidRuntime(265): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736) 01-25 11:50:57.697: E/AndroidRuntime(265): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752) 01-25 11:50:57.697: E/AndroidRuntime(265): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 01-25 11:50:57.697: E/AndroidRuntime(265): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 01-25 11:50:57.697: E/AndroidRuntime(265): at android.os.Handler.dispatchMessage(Handler.java:99) 01-25 11:50:57.697: E/AndroidRuntime(265): at android.os.Looper.loop(Looper.java:126) 01-25 11:50:57.697: E/AndroidRuntime(265): at android.app.ActivityThread.main(ActivityThread.java:3997) 01-25 11:50:57.697: E/AndroidRuntime(265): at java.lang.reflect.Method.invokeNative(Native Method) 01-25 11:50:57.697: E/AndroidRuntime(265): at java.lang.reflect.Method.invoke(Method.java:491) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 01-25 11:50:57.697: E/AndroidRuntime(265): at dalvik.system.NativeStart.main(Native Method) 01-25 11:50:57.697: E/AndroidRuntime(265): Caused by: java.lang.NullPointerException 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.ui.UriImage.(UriImage.java:68) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.model.ImageModel.initModelFromUri(ImageModel.java:82) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.model.ImageModel.(ImageModel.java:64) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.data.WorkingMessage.changeMedia(WorkingMessage.java:462) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.data.WorkingMessage.setAttachment(WorkingMessage.java:363) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.ui.ComposeMessageActivity.addImage(ComposeMessageActivity.java:2747) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.ui.ComposeMessageActivity.addAttachment(ComposeMessageActivity.java:2883) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.ui.ComposeMessageActivity.handleSendIntent(ComposeMessageActivity.java:2812) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.ui.ComposeMessageActivity.initialize(ComposeMessageActivity.java:1776) 01-25 11:50:57.697: E/AndroidRuntime(265): at com.android.mms.ui.ComposeMessageActivity.onCreate(ComposeMessageActivity.java:1704) 01-25 11:50:57.697: E/AndroidRuntime(265): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 01-25 11:50:57.697: E/AndroidRuntime(265): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700) 01-25 11:50:57.697: E/AndroidRuntime(265): ... 11 more

有人可以更正代码吗?添加了 MANIFEST.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shareimage"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.shareimage.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-permission android:name="android.permission.INTERNET"/>

</manifest>

最佳答案

使用此代码(我已编辑您的代码):

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");


Uri imageUri = Uri.parse("android.resource://com.example.mm/drawable/ic_launcher");
Log.i("imageUri",""+imageUri);
share.putExtra(Intent.EXTRA_STREAM,imageUri);
startActivity(Intent.createChooser(share, "Share Image"));
}

}

enter image description here

关于java - 无法使用 ShareIntent 从 Android 中的可绘制文件夹发送图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21347750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com