Android画像カットソリューション-アルバムから

6897 ワード

Storage Access Frameworkを見ると、アルバムの画像をロードするプログラムの断片が入っていて、システムバージョンの問題かもしれません.結果を返すことができません.ここで古いバージョンに適用する方法を見つけます.
 
Androidの開発では、アルバムから切り取る作業を簡単に行うためにIntentを呼び出すことができます.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");

追加のオプションは次のとおりです.
オプション
データ型
説明
crop
String
トリミング信号を送信
aspectX
int
X方向の割合
aspectY
int
Y方向の割合
outputX
int
クリップ領域の幅
outputY
int
裁断領域の高さ
scale
boolean
スケールを保持するかどうか
return-data
boolean
Bitmapにデータを保持して戻すかどうか
data
Parcelable
対応するBitmapデータ
circleCrop
String
円形裁断領域?
MediaStore.EXTRA_OUTPUT ("output")
URI
URIを対応するfile:///...
 
このreturn-dataは比較的困惑している.
スクリーンショットデータを受信するには、次の2つの方法があります.
方法1:return-dataをtrueに設定し、データを直接Intentを通じてonActivity Resultに戻す.以下のようにする.
public void SelectSmallImg(View view){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 800);
        intent.putExtra("outputY", 800);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        startActivityForResult(intent, CHOOSE_SMALL_PICTURE);
    }

方法2:return-dataをfalseとし、MediaStoreを利用する.EXTRA_OUTPUTタグは、外部の一時URIにデータを格納し、onActivity Resultで読み出す.もちろん、事前にファイルを指すURIを用意しておきますが、sdcardがあれば問題なく、なければどうなりますか???
public void SelectLargeImg(View view){
        // this is for android 4.4 ??
        //Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT,null);
        intent.setType("image/*");
        intent.putExtra("crop", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        //intent.putExtra("outputX", 400);  //         X     。
        //intent.putExtra("outputY", 400);  //      Y      。
        intent.putExtra("scale", true);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent,CHOOSE_BIG_PICTURE);
    }

onActivity Resultでは、次のように対処できます.
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent resultData){
        switch(requestCode){
            case CHOOSE_BIG_PICTURE:
                if(resultCode == Activity.RESULT_OK && resultData != null){
                    Log.i(DEBUG_FLAG,imageUri.toString());
                    Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap (      URI   bitmap   )
                    if(bitmap == null){
                        Log.i(DEBUG_FLAG,"BIG_PICTURE NULL");
                    }else{
                        imageView.setImageBitmap(bitmap);
                        Log.i(DEBUG_FLAG , "Size:"+bitmap.getWidth()+" X "+bitmap.getHeight());
                    }
                    
                }
                break;
            case CHOOSE_SMALL_PICTURE:
                if(resultCode == Activity.RESULT_OK && resultData != null){
                    Bitmap bitmap = resultData.getParcelableExtra("data");
                    if(bitmap == null){
                        Log.i(DEBUG_FLAG,"SMALL_PICTURE NULL");
                    }else{
                        imageView.setImageBitmap(bitmap);
                        Log.i(DEBUG_FLAG , "Size:"+bitmap.getWidth()+" X "+bitmap.getHeight());
                    }
                }
                break;
            default :
                break;
        }
    }

自分でURIをbitmapに変換する関数を書きます.
Bitmap decodeUriAsBitmap(Uri uri){
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return null;
            }
            return bitmap;
    }

============================
まとめますと、メモリの考慮に基づいて、画像が大きい場合、androidのスクリーンショット機能は160*160のサムネイルを返し、外部URIでしかデータを受信できません.
 
この文書のほとんどの参考:http://blog.csdn.net/floodingfire/article/details/8144615
APIガイドのStorage Access FrameworkはAPI 4に基づく.4のバージョンは、中の最後の部分にカスタムDocument Providerの構築について、ここでも記録します.(例えば、独自に実装されたクラウドストレージ)