Failed to Send Image File using ShareIntent from d

2019-06-09 14:24发布

问题:

I want to share image using intent ,i have used code from 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"));
}}

i am getting following error when trying to run this code in emulator:

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

Can someone pls correct code? ADDED 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>

回答1:

use this code(i have edited your code):

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"));
    }

}