@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d("onActivityResult","success"); // if (resultCode == RESULT_OK) { // 如果返回数据 if (requestCode == REQUEST_IMAGE_CAPTURE) { try { //查询的条件语句 String selection = MediaStore.Images.Media.DISPLAY_NAME + "=? "; //查询的sql //Uri:指向外部存储Uri //projection:查询那些结果 //selection:查询的where条件 //sortOrder:排序 Log.d("onActivityResulttry","success"); Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID} ,selection,new String[]{fileName},null); Log.d("onActivityResultcursor","success"); if (cursor != null && cursor.moveToFirst()) { do { Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(0)); Log.d("luingssd","@"+uri); ivShowPicture = findViewById(R.id.image_view_2); ivShowPicture.setImageURI(uri); }while (cursor.moveToNext()); }else { Toast.makeText(this,"no photo",Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }
时间: 2024-02-10 09:12:33 浏览: 92
Android中两个类让你再也不用实现onActivityResult()
这段代码是在 Activity 接收到从其他页面返回的结果后,执行的回调函数。在该函数中,首先判断返回结果是否成功(resultCode == RESULT_OK),然后根据请求码(requestCode)判断返回的是哪个操作的结果。如果是拍照操作(REQUEST_IMAGE_CAPTURE),则根据文件名查询系统相册中的图片,并将其显示在 ImageView 控件中。通过 getContentResolver().query() 方法查询系统相册中的图片,得到其 Uri,再将 Uri 传递给 ImageView 控件的 setImageURI() 方法进行显示。
阅读全文