无法在活动中显示从内置库中选择的图像到imageview

时间:2012-08-31 02:03:26

标签: imageview gallery

我有一个imageview和一个带有4个edittext字段的按钮上传头像(当我向数据库插入条目时它们正常。然后当我点击上传头像时,它会进入内置库,我可以选择图像到在imageview上显示。但是,当我点击内置图库中的图像时,它不会在imageview上显示图像。相反,它会给我一个错误。

这是我的Main.java:

   public class Main extends Activity {
   DBAdapter db = new DBAdapter (this);
   private ImageView img;
  private String selectedImagePath;
   private static final int SELECT_IMAGE=0;

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

    final ImageView img = (ImageView) findViewById(R.id.imageViewavatar);
    Button upload = (Button) findViewById(R.id.buttonuimage);
    upload.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Intent imageintent = new Intent();
            imageintent.setType("image/*");
            imageintent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(imageintent, "Select Picture"), SELECT_IMAGE);


        }
    });


Button save = (Button) findViewById(R.id.savebutton);
save.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        boolean success = true;
        try
        {
        db.open();
            EditText editTextfirstname = (EditText) findViewById(R.id.editText1);
            EditText editTextlastname = (EditText) findViewById(R.id.editText2);
            EditText editTextcity = (EditText) findViewById(R.id.editText3);
            EditText editTextstate = (EditText) findViewById(R.id.editText4);


            String firstnameValue = editTextfirstname.getText().toString();
            String lastnameValue = editTextlastname.getText().toString();
            String cityValue = editTextcity.getText().toString();
            String stateValue = editTextstate.getText().toString();
            String imageValue = selectedImagePath.toString();

            db.insertContact(firstnameValue, lastnameValue, cityValue, stateValue, imageValue);
            }
        catch (Exception e){
            success = false;    
        }finally{
            if(success){
            Toast toast = Toast.makeText(Main.this, "Successfully added contact", 5000);
            toast.show();
            MediaPlayer mp = MediaPlayer.create(Main.this, R.raw.beep);
            mp.start();
            Intent intnt = new Intent(Main.this, Main.class);
            startActivity(intnt);



                }

            }
        }
    private void show_mesg(String msg) {
        Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
    }
    });
    }
 public void added (View v){
Intent i = new Intent(Main.this, Second.class);
startActivity(i);

 }

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_IMAGE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
            img.setImageBitmap(bm);


        }
    }
 }

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

这就是在LogCat上显示的内容:

08-31 09:52:21.674: E/AndroidRuntime(459): FATAL EXCEPTION: main
08-31 09:52:21.674: E/AndroidRuntime(459): java.lang.RuntimeException: Failure delivering      result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images  /media/2 }} to activity {com.dtan.testcontacts/com.dtan.testcontacts.Main}:  java.lang.NullPointerException
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
 08-31 09:52:21.674: E/AndroidRuntime(459):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.os.Looper.loop(Looper.java:123)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-31 09:52:21.674: E/AndroidRuntime(459):  at java.lang.reflect.Method.invokeNative(Native Method)
08-31 09:52:21.674: E/AndroidRuntime(459):  at java.lang.reflect.Method.invoke(Method.java:507)
08-31 09:52:21.674: E/AndroidRuntime(459):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-31 09:52:21.674: E/AndroidRuntime(459):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-31 09:52:21.674: E/AndroidRuntime(459):  at dalvik.system.NativeStart.main(Native Method)
08-31 09:52:21.674: E/AndroidRuntime(459): Caused by: java.lang.NullPointerException
08-31 09:52:21.674: E/AndroidRuntime(459):  at com.dtan.testcontacts.Main.onActivityResult(Main.java:113)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.Activity.dispatchActivityResult(Activity.java:3908)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
08-31 09:52:21.674: E/AndroidRuntime(459):  ... 11 more

1 个答案:

答案 0 :(得分:0)

我明白了!!请参阅下面的编辑代码:

我更换了:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    if (requestCode == SELECT_IMAGE) {
        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
        img.setImageBitmap(bm);

使用:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    if (requestCode == SELECT_IMAGE) {
        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        File f = new File (selectedImagePath);
            Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath());
            img.setImageBitmap(bm);