无法使用共享意图发送所选图像

时间:2014-04-22 19:52:24

标签: java android android-intent file-sharing

我正在创建一个应用程序,该应用程序提供MainActivity.java中某些照片的网格视图,之后在选择照片时,新类将定向到FullImageActivity.java。 之后,我使用ShareIntents在社交应用上分享所选图像。 虽然这样做应用程序在我的设备中运行崩溃但是在PC模拟器中应用程序不会崩溃并发送空白消息。

这是我的**MainActivity.java** //represents the grid view.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridView = (GridView) findViewById(R.id.grid_view); //initial grid view welcome screen


    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {


            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);   //switching classes

            i.putExtra("id", position);
            startActivity(i);
        }
    });
}

}

以下是**FullImageActivity.java**

@SuppressLint("SdCardPath")
public class FullImageActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    Intent i = getIntent();

    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(imageAdapter.mThumbIds[position]);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.id.full_image_view); //file to be saved as per user selection
    File sd = Environment.getExternalStorageDirectory();
    String fileName = "test.png"; //saved as png file
    File dest = new File(sd, fileName);
    try {
        FileOutputStream out;
        out = new FileOutputStream(dest);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    switch (item.getItemId()) {
        case R.id.item:
            Uri uri = Uri.fromFile(dest);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.setType("image/png");
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share))); //shared via Intent
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

显示我用于创建full_image.xml

的选定图像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/full_image_view"
    style="@style/myImageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    tools:ignore="ContentDescription" />

我将图像存储在此类中声明的数组中 **ImageAdapter.java**

package com.jai.desimeme;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

// Keeping all Images in array
public  Integer[] mThumbIds = {

        R.drawable.rage_0001,R.drawable.rage_178,

};

// Constructor
public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
    return imageView;
}

}

所以,有LogCat 更新

04-23 05:03:07.907: W/EGL_emulation(1172): eglSurfaceAttrib not implemented
04-23 05:03:08.137: D/dalvikvm(1172): GC_FOR_ALLOC freed 33K, 3% free 4846K/4952K, paused 39ms, total 40ms
04-23 05:03:10.607: W/EGL_emulation(1172): eglSurfaceAttrib not implemented
04-23 05:03:11.767: D/AndroidRuntime(1172): Shutting down VM
04-23 05:03:11.777: W/dalvikvm(1172): threadid=1: thread exiting with uncaught exception (group=0xb4a8cb90)
04-23 05:03:11.777: E/AndroidRuntime(1172): FATAL EXCEPTION: main
04-23 05:03:11.777: E/AndroidRuntime(1172): Process: com.jai.desimeme, PID: 1172
04-23 05:03:11.777: E/AndroidRuntime(1172): java.lang.NullPointerException
04-23 05:03:11.777: E/AndroidRuntime(1172):     at com.jai.desimeme.FullImageActivity.onOptionsItemSelected(FullImageActivity.java:55)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.app.Activity.onMenuItemSelected(Activity.java:2599)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:177)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.os.Handler.handleCallback(Handler.java:733)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.os.Looper.loop(Looper.java:137)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at android.app.ActivityThread.main(ActivityThread.java:4998)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invokeNative(Native Method)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invoke(Method.java:515)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-23 05:03:11.777: E/AndroidRuntime(1172):     at dalvik.system.NativeStart.main(Native  Method)
04-23 05:03:15.517: I/Process(1172): Sending signal. PID: 1172 SIG: 9
04-23 05:03:16.447: D/dalvikvm(1200): GC_FOR_ALLOC freed 60K, 4% free 3117K/3244K,   paused 27ms, total 30ms
04-23 05:03:16.707: D/(1200): HostConnection::get() New Host Connection established 0xb8c69818, tid 1200
04-23 05:03:16.967: W/EGL_emulation(1200): eglSurfaceAttrib not implemented
04-23 05:03:16.977: D/OpenGLRenderer(1200): Enabling debug mode 0
04-23 05:03:17.057: D/dalvikvm(1200): GC_FOR_ALLOC freed 5K, 4% free 3206K/3336K, paused 21ms, total 22ms
04-23 05:03:17.127: D/dalvikvm(1200): GC_FOR_ALLOC freed <1K, 4% free 3608K/3740K, paused 20ms, total 21ms
04-23 05:03:17.247: D/dalvikvm(1200): GC_FOR_ALLOC freed <1K, 4% free 4027K/4164K, paused 20ms, total 20ms
04-23 05:03:17.357: D/dalvikvm(1200): GC_FOR_ALLOC freed 1K, 4% free 4345K/4480K, paused 21ms, total 21ms
04-23 05:03:17.447: I/Choreographer(1200): Skipped 63 frames!  The application may be doing too much work on its main thread.

LogCat在模拟器上进行跟踪,但不会崩溃,但我无法通过我的设备跟踪此logs。 :( 已解决

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jai.desimeme"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1.1" >

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

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

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

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>
    <!-- FullImageActivity -->
    <activity android:name="com.jai.desimeme.FullImageActivity" >
    </activity>
    <activity
        android:name="com.jai.desimeme.About"
        android:theme="@android:style/Theme.Dialog" >
    </activity>
    <activity
        android:name="com.jai.desimeme.ShareActivity"
        android:label="@string/title_activity_share" >
    </activity>
</application>

</manifest>

请指导我完成这个...... :) 谢谢!

1 个答案:

答案 0 :(得分:3)

错误就在这一行:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.id.full_image_view);

在此方法中,第二个参数是Drawable的资源ID,而不是ImageView的资源ID。 相反,你应该这样做:

ImageView image = (ImageView) findViewById(R.id.full_image_view);
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

它应该有用。