从Android中的drawable文件夹使用ShareIntent发送图像文件失败?

时间:2014-01-25 07:05:31

标签: java android android-intent uri

我想使用意图分享图片,我使用了来自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):致命异常:主01-25   11:50:57.697:E / AndroidRuntime(265):java.lang.RuntimeException:   无法开始活动   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):引起: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更多

有人可以使用正确的代码吗? 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 个答案:

答案 0 :(得分:1)

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

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