上传pic时失败,传递结果resultinfo

时间:2014-07-18 08:06:40

标签: android image sqlite bytearray

我想从图库中提取图像并将其存储在数据库中,但是这个错误显示出来,请看这个。这是我的日志猫,但是当我将开关盒更改为0时,没有出现错误,但也没有上传图像。

 07-18 03:58:59.710: E/AndroidRuntime(29499): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/18 }} to activity {com.nisani.angel/com.nisani.angel.Pic}: java.lang.NullPointerException

这是我的代码。

button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 

        }
    });
button2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
    Intent i = new Intent(Pic.this,Categories.class);
startActivity(i);

}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case ACTIVITY_SELECT_IMAGE:
        if(resultCode == RESULT_OK && null != imageReturnedIntent){  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream;

                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);

                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
            yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, out);

            byte[] buffer=out.toByteArray();

            SQLiteDatabase db=this.openOrCreateDatabase("Angel", MODE_PRIVATE, null);
            try {db.execSQL("CREATE TABLE IF NOT EXISTS "  + "DBTABLE" + "("+ 
                       "NAME" + " TEXT," + 
                       "IMAGE" + " BLOB);");
            }
            catch (SQLiteException e){
                e.printStackTrace();
            }
            ContentValues values = new ContentValues();
             HashMap<String, String> user = session.getUserDetails();

                String name = user.get(SessionManager.KEY_NAME);
                values.put("NAME", name);
            values.put("IMAGE", buffer); 
            long row_id=db.insert("DBTABLE", null, values);

            if(row_id==(-1)){
                String tag=null;
                Log.i(tag,"error in inserting image");
            }
                }
                catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



        }
    }
}

}

0 个答案:

没有答案