@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { ContentResolver resolver = getContentResolver(); 相冊中獲取的uri正常 if (requestCode == 0) { try { Uri uri = data.getData(); Log.v("uri_xiangce",""+uri); Cursor cursor = resolver.query(uri, null, null, null, null); cursor.moveToFirst(); String imgNo = cursor.getString(0); //圖片編號 imgPath = cursor.getString(1); //圖片文件路徑 Log.v("imagpath", ""+imgPath); String imgSize = cursor.getString(2); //圖片大小 Log.v("imgSize", ""+imgSize); String imgName = cursor.getString(3); //圖片文件名 Log.v("imgName", ""+imgName); try { InputStream in = resolver.openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(in); ivPortrait.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } 但是相機(jī)中的uri就是一個null else if(requestCode ==1){ try { super.onActivityResult(requestCode, resultCode, data); Uri uri = data.getData(); Log.v("uri_xiangji", ""+uri); Bundle extras = data.getExtras(); myBitmap = (Bitmap) extras.get("data"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos); mContent=baos.toByteArray(); Cursor cursor = resolver.query(uri, null, null, null, null); cursor.moveToFirst(); String imgNo = cursor.getString(0); //圖片編號 imgPath = cursor.getString(1); //圖片文件路徑 Log.v("imagpath", ""+imgPath); String imgSize = cursor.getString(2); //圖片大小 Log.v("imgSize", ""+imgSize); String imgName = cursor.getString(3); //圖片文件名 Log.v("imgName", ""+imgName); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //把得到的圖片綁定在控件上顯示 ivPortrait.setImageBitmap(myBitmap); } }
發(fā)表評論