Question

I'm creating an app which provide a grid view of some photos in MainActivity.java, after that on selection of photo a new class is directed to FullImageActivity.java. after that i used ShareIntents to share the selected image over social apps. while doing so the app runs crashes in my device however in pc simulator the app does not crashes and send blank message instead.

Here is my **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);
        }
    });
}

}

Here is the **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);
    }
}

}

to display the slected image i used to create 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" />

i stored the images in array which is declared in this class **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;
}

}

So, there is the LogCat UPDATED

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.

this LogCat is traced on emulator which does not get crashed however i am unable to trace this logs over my device. :( SOLVED

and the 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>

please guide me through this...:) Thank You!

Était-ce utile?

La solution

The error is at this line :

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

In this method the second argument is the resource id of the Drawable not of the ImageView. Instead you should do this :

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

It should work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top