从图库中选择图像后出现空指针异常

时间:2013-10-21 22:02:50

标签: android nullpointerexception

我的应用程序安装正常,但是当我从图库中选择一个图像放入imageView时,应用程序强制关闭,我得到这个空指针异常:

10-21 15:53:31.340: E/AndroidRuntime(24994): FATAL EXCEPTION: main
10-21 15:53:31.340: E/AndroidRuntime(24994): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat=content://media/external/images/media/122 typ=image/png (has extras) }} to activity {com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.Personalize}: java.lang.NullPointerException
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2974)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3026)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.app.ActivityThread.access$2000(ActivityThread.java:135)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1071)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.os.Looper.loop(Looper.java:150)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.app.ActivityThread.main(ActivityThread.java:4333)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at java.lang.reflect.Method.invokeNative(Native Method)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at java.lang.reflect.Method.invoke(Method.java:507)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at dalvik.system.NativeStart.main(Native Method)
10-21 15:53:31.340: E/AndroidRuntime(24994): Caused by: java.lang.NullPointerException
10-21 15:53:31.340: E/AndroidRuntime(24994):    at com.example.awesomefilebuilderwidget.Personalize.onActivityResult(Personalize.java:76)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.app.Activity.dispatchActivityResult(Activity.java:4053)
10-21 15:53:31.340: E/AndroidRuntime(24994):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2970)
10-21 15:53:31.340: E/AndroidRuntime(24994):    ... 11 more

这是我的Personalize.java:

package com.example.awesomefilebuilderwidget;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Personalize extends Activity implements OnClickListener {
Button button;
ImageView image;
ImageView image2;
Button btnChangeImage;
Button btnChangeImageForIcon;
private static final int SELECT_PICTURE = 1;
private static final int SELECT_PICTURE_2 = 2;
private String  selectedImagePath;

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

Button btnChangeImage = (Button) findViewById(R.id.btnChangeImage);    
btnChangeImage.setOnClickListener(this);
Button btnChangeImageForIcon = (Button) findViewById(R.id.btnChangeImageForIcon); 
btnChangeImageForIcon.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 intent.addCategory(Intent.CATEGORY_OPENABLE);
 startActivityForResult(intent, SELECT_PICTURE);


};

public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE)
{
    Uri selectedImageUri = data.getData();
    selectedImagePath = getPath(selectedImageUri);
    Bitmap b1 = getAndDecodeImage(selectedImagePath);
    if(b1 != null){
        image.setImageBitmap(b1);
    }           
} else if (requestCode == SELECT_PICTURE_2)
{
    Uri selectedImageUri = data.getData();
    selectedImagePath = getPath(selectedImageUri);
    Bitmap b2 = getAndDecodeImage(selectedImagePath);
    if(b2 != null){
        image2.setImageBitmap(b2);
    }
}    
}
}

private Bitmap getAndDecodeImage(String  selectedImagePath){
try {
    FileInputStream fileis=new FileInputStream(selectedImagePath);
    BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
    byte[] bMapArray= new byte[bufferedstream.available()];
    bufferedstream.read(bMapArray);
    Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);

    if (fileis != null) 
    {
        fileis.close();
    }
    if (bufferedstream != null) 
    {
        bufferedstream.close();
    }
    return bMap;
} catch (FileNotFoundException e) {                 
    e.printStackTrace();
} catch (IOException e) {                   
    e.printStackTrace();
}   
return null;
}


public boolean saveImageToInternalStorage(Bitmap image) {
   try {
      FileOutputStream fos = this.openFileOutput("desiredFilename.png",     Context.MODE_PRIVATE);
      image.compress(Bitmap.CompressFormat.PNG, 100, fos);
      fos.close();   
      return true;
   } catch (Exception e) {
   return false;
   }
}
}

这是第76行:

image.setImageBitmap(b1);

1 个答案:

答案 0 :(得分:0)

你得到一个NPE因为你的图像变量从未被初始化,所以默认情况下它是一个空指针,因此当你调用它上面的函数时,你会得到一个异常。

尝试将其初始化为您想要的,并且NPE应该消失。