0

I am trying to display an image from my phone using ImageView. Here is my ImageActivity activity:

package com.example.apptest1;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View; 
import android.widget.ImageView;

public class ImageActivity extends Activity {

private static final int REQUEST_CODE = 0;
private Bitmap bitmap;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
     ImageView imageView = (ImageView) findViewById(R.id.result);

}

public void pickImage(View View) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, REQUEST_CODE);
}

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        InputStream stream = null;
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
          try {
            // recyle unused bitmaps
            if (bitmap != null) {
              bitmap.recycle();
            }
            stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);

            imageView.setImageBitmap(bitmap);
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } finally {
            if (stream != null)
              try {
                stream.close();
              } catch (IOException e) {
                e.printStackTrace();
              }
          }
      }



 }

And here is the activity_image.xml

<LinearLayout 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"
tools:context="${relativePackage}.${activityClass}" 
android:orientation="vertical">


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="pickImage"
    android:text="Button" >
</Button>

<ImageView
    android:id="@+id/result"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
</ImageView>


</LinearLayout>

But when I try to run the application,it displays the below logcat and stops abruptly LogCat:

    05-28 23:11:48.226: E/AndroidRuntime(6115): FATAL EXCEPTION: main
05-28 23:11:48.226: E/AndroidRuntime(6115): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,  request=0, result=-1, data=Intent { dat=content://media/external/images/media/1242 flg=0x1 }} to activity   {com.example.apptest1/com.example.apptest1.ImageActivity}: java.lang.NullPointerException
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3525)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3568)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.access$1100(ActivityThread.java:162)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.os.Handler.dispatchMessage(Handler.java:107)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.os.Looper.loop(Looper.java:194)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.main(ActivityThread.java:5371)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at java.lang.reflect.Method.invoke(Method.java:525)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run   (ZygoteInit.java:833)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at dalvik.system.NativeStart.main(Native Method)
05-28 23:11:48.226: E/AndroidRuntime(6115): Caused by: java.lang.NullPointerException
05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.example.apptest1.ImageActivity.onActivityResult(ImageActivity.java:48)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.Activity.dispatchActivityResult(Activity.java:5311)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3521)
05-28 23:11:48.226: E/AndroidRuntime(6115):     ... 11 more

I have debuged and checked that ImageView is null but not able to figure out why/how.Can someone please clear my doubt.Thanks in advance

2 Answers 2

3

Simple scope issue.

This line

ImageView imageView = (ImageView) findViewById(R.id.result);

needs to be

imageView = (ImageView) findViewById(R.id.result);

Why? Because the way you're doing it now means that you've declared a new imageView instead of using the one already declared previously. You've left that previously declared imageView untouched, and instead made a brand new one that's absolutely useless and dissapears after the onCreate() finishes. When you call imageView.setImageBitmap(bitmap);, you'll get an NPE because it tries to use the imageView declared in your class.

Sign up to request clarification or add additional context in comments.

Comments

1

in your onCreate method you are creating a local variable by:-

ImageView imageView = (ImageView) findViewById(R.id.result);

That hides your global imageView member. So, do below in onCreate:-

imageView = (ImageView) findViewById(R.id.result);

And the null pointer should go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.