android 4.3 그림 선택 "그림을 불러올 수 없습니다"

7208 단어 테크니컬
막 박문을 쓰기 시작했으니 소홀한 점이 있을 수 없으니 양해해 주십시오.쓸데없는 말은 그만하고 이틀 동안 프로젝트를 할 때 4.3버전의 시스템이 앨범에서 사진을 캡처할 때 그림을 불러올 수 없다는 것을 발견했다.일부 자료를 뒤적였는데 안드로이드 4.4 이전에 시스템 갤러리를 호출하여 그림을 받은 Uri와 4.4 이상의 Uri는 큰 차이가 있는데 Intent만 사용했다.ACTION_GET_CONTENT가 4.4 이상인 버전에서는 받은 Uri를 해석할 수 없습니다.따라서 갤러리를 호출하기 전에 버전의 영어 Build.VERSION_CODES.KITKAT 이상 버전, 공식 권장사항은 ACTIONOPEN_DOCUMENT.코드는 다음과 같습니다.
//                   


4
private void getAlbum() {
    String intentactiong = "";
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {//4.4   
        intentactiong = Intent.ACTION_PICK;
    } else {//4.4   
        intentactiong = Intent.ACTION_GET_CONTENT;
    }
    Intent openAlbumIntent = new Intent(intentactiong);
    openAlbumIntent.setDataAndType(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
    startActivityForResult(openAlbumIntent, FLAG_SELECT_PHOE);
}
다음은 그림을 캡처하는 방법입니다. 두 버전의 그림은 선택 경로가 다르기 때문에 서로 다른 버전 시스템에 맞게 그림을 주동적으로 저장했습니다.
 
  
//     
public void cropImage(Uri uri, int outputX, int outputY, int requestCode) {
    if (uri != null) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {//4.4   
//          
            String fileName = String.valueOf(System.currentTimeMillis()) + ".png";
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("tempName", fileName);
            editor.commit();
//    Uri  
            Uri imageUri = Uri.fromFile(new File(Environment
                    .getExternalStorageDirectory(), fileName));
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        }
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("outputFormat", "PNG");
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, requestCode);
    }
}
 
  
 onactivityresult     
 
  
onActivityResult
    Uri uri = null;
    if (data != null) {
        uri = data.getData();
        System.out.println("Data");
    } else {
        System.out.println("File");
        String fileName = getSharedPreferences("temp",
                Context.MODE_PRIVATE).getString("tempName",
                "");
        uri = Uri.fromFile(new File(Environment
                .getExternalStorageDirectory(), fileName));
    }
    try {
        Bitmap bitmaps = MediaStore.Images.Media.getBitmap(
                this.getContentResolver(), uri);
        img_1.setImageBitmap(bitmaps);

    } catch (IOException e) {
        e.printStackTrace();
    }
    cropImage(uri, PHOTOSIZE, PHOTOSIZE, FLAG_CROP_PHONE);
   

소스 코드 추가https://github.com/longyuan02/Interceptpictures

좋은 웹페이지 즐겨찾기