萬(wàn)幸的是CROP提供的選項(xiàng)中,還有一個(gè),可以自定義裁切輸出的圖片存儲(chǔ)位置。利用這一點(diǎn),就可以規(guī)避Intent攜帶信息的不靠譜所造成的吃飯不香。
01    //發(fā)送請(qǐng)求
02    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
03    intent.setType("image/*");
04    intent.putExtra("crop", "true");
05    intent.putExtra("aspectX", 1);
06    intent.putExtra("aspectY", 1);
07    intent.putExtra("outputX", 1000);
08    intent.putExtra("outputY", 1000);
09    intent.putExtra("scale", true);
10    intent.putExtra("return-data", false);
11    intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
12    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
13    intent.putExtra("noFaceDetection", true);
14    startActivityForResult(intent, PHOTORESOULT);
15    
16    //選擇存儲(chǔ)位置
17    Uri tempPhotoUri;
18    
19    private Uri getTempUri() {
20        Uri tempPhotoUri = Uri.fromFile(getTempFile());
21        return tempPhotoUri;
22    }
23    
24    private File getTempFile() {
25        if (isSDCARDMounted()) {
26    
27        File f = new File(Environment.getExternalStorageDirectory(),"temp.jpg");
28        try {
29            f.createNewFile();
30        } catch (IOException e) {
31                ...
32            }
33        return f;
34        }
35        return null;
36    }
37    
38    // 注意,在我做的測(cè)試中,使用內(nèi)部緩存是無(wú)法正確寫入裁切后的圖片的。請(qǐng)大家有興趣的測(cè)試一番,看是否有意外。
39    // 系統(tǒng)是用全局的ContentResolver來(lái)做這個(gè)過(guò)程的文件io操作,app內(nèi)部的存儲(chǔ)被忽略。(猜測(cè))
40    /*
41        File f = new File(getCacheDir(), "temp.jpg");
42        try {
43            f.createNewFile();
44        } catch (IOException e) {
45           ...
46        }
47        return f;
48        }
49    */
50    
51    private boolean isSDCARDMounted(){
52        String status = Environment.getExternalStorageState();
53    
54        if (status.equals(Environment.MEDIA_MOUNTED)){
55            return true;
56        }
57        return false;
58    }
59    
60    // 處理結(jié)果
61    //
62    Bitmap bitmap = null;
63    try {              
64        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(tempPhotoUri));
65    } catch (FileNotFoundException e) {
66        e.printStackTrace();
67    }
Android相關(guān)內(nèi)容: