androidの写真とカスタムマルチ選択アルバムを取得


システムのカメラ機能を取得して写真を撮るのは難しくありませんが、注意しなければならないのは、写真を撮って戻った写真が記憶の経路を指定していなければ、システムは自動的にsdカードに保存され、写真を撮ったサムネイルが得られ、フレームが失われ、表示がぼやけているので、システムのカメラを呼び出して写真を撮った後、パスを指定して保存し、必要なときに取りに行きます.アルバムを呼び出すには、アルバムの複数選択が必要でしたが、システムアルバムは単一選択しかできないので、アルバムをカスタマイズしました.直接startActivityなので、アルバムとシステムカメラを取得するIntentがどのように書かれているかを見てみましょう.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("    ");
String[] items = {"     ", "  "};
builder.setNegativeButton("  ", null);
builder.setItems(items, new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
      switch (which) {
         case CHOOSE_PICTURE://       
            Intent intent = new Intent(SampleSendActivity.this,SampleCameraActivity.class);
            startActivityForResult(intent, ACTION_IMAGE_PICTURE);
            break;
         case TAKE_PICTURE://   
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            /**
             *             
              */
String name = new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
           File file = new File("/sdcard/myImage/");
           file.mkdirs();//      
            mFilePath = "/sdcard/myImage/"+name;
           //     
            Uri uri = Uri.fromFile(new File(mFilePath));
           //             intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
           intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
           //       
            startActivityForResult(intent, ACTION_IMAGE_CAMEREA);
            break;
      }
   }
});
builder.create().show();

システムアルバムを呼び出して指定したfileを取得し、直接パスでimageviewに表示します.
if (requestCode == ACTION_IMAGE_CAMEREA && resultCode == Activity.RESULT_OK) {
   String storageState = Environment.getExternalStorageState();
   if (!storageState.equals(Environment.MEDIA_MOUNTED)) {
      return;
   }
   // /sdcard
   mFilePath = "/storage/emulated/0" + mFilePath.substring(7);
   BitmapUtil.loadUriImage(SampleSendActivity.this, mFilePath, tagHolder.iv_pinkaddpig);
   //   bitmaputil      ,         imageview ,    
}

カスタムマルチ選択アルバムの実装を見てみましょう
public class SampleCameraActivity extends Activity implements OnImageDirSelected {
    private ProgressDialog mProgressDialog;
    /**
     *            
     */
    private int mPicsSize;
    /**
     *           
     */
    private File mImgDir;
    /**
     *      
     */
    private List mImgs;
    private GridView mGirdView;
    private MyAdapter mAdapter;
    /**
     *      */
    private HashSet mDirPaths = new HashSet();
    /**
     *             
     */
    private List mImageFloders = new ArrayList();
    private RelativeLayout mBottomLy;
    private TextView mChooseDir;
    private TextView mImageCount;
    int totalCount = 0;
    private int mScreenHeight;
    private ListImageDirPopupWindow mListImageDirPopupWindow;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mProgressDialog.dismiss();
            //  View    
            data2View();
            //          popupWindw
            initListDirPopupWindw();
        }
    };
    private TextView tv_samplecamera_confirm;
    private TextView tv_return;
    /**
     *  View    
     */
    private void data2View() {
        if (mImgDir == null) {
            Toast.makeText(getApplicationContext(), "",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        mImgs = Arrays.asList(mImgDir.list());
        /**
         *                     ,           ;
         */
        mAdapter = new MyAdapter(getApplicationContext(), mImgs,
                R.layout.view_cameragrid_item, mImgDir.getAbsolutePath());
        mAdapter.setIamgeNull();
        mGirdView.setAdapter(mAdapter);
        mImageCount.setText(totalCount + " ");
    };
    /**
     *          popupWindw
     */
    private void initListDirPopupWindw() {
        mListImageDirPopupWindow = new ListImageDirPopupWindow(
                (int) LayoutParams.MATCH_PARENT, (int) (mScreenHeight * 0.7),
                mImageFloders, LayoutInflater.from(getApplicationContext())
                .inflate(R.layout.view_cameralist_dir, null));
        mListImageDirPopupWindow.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss() {
                //         
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1.0f;
                getWindow().setAttributes(lp);
            }
        });
        //           
        mListImageDirPopupWindow.setOnImageDirSelected(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_camera);
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        mScreenHeight = outMetrics.heightPixels;
        getActionBar().hide();
        initView();
        getImages();
        initEvent();
    }

    /**
     *   ContentProvider        ,                   ,    jpg        
     */
    private void getImages() {
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "      ", Toast.LENGTH_SHORT).show();
            return;
        }
        //      
        mProgressDialog = ProgressDialog.show(this, null, "    ...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                String firstImage = null;
                Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                ContentResolver mContentResolver = SampleCameraActivity.this
                        .getContentResolver();
                //    jpeg png   
                Cursor mCursor = mContentResolver.query(mImageUri, null,
                        MediaStore.Images.Media.MIME_TYPE + "=? or "
                                + MediaStore.Images.Media.MIME_TYPE + "=?",
                        new String[]{"image/jpeg", "image/png"},
                        MediaStore.Images.Media.DATE_MODIFIED);
                Log.e("TAG", mCursor.getCount() + "");
                while (mCursor.moveToNext()) {
                    //        
                    String path = mCursor.getString(mCursor
                            .getColumnIndex(MediaStore.Images.Media.DATA));
                    Log.e("TAG", path);
                    //           
                    if (firstImage == null)
                        firstImage = path;
                    //           
                    File parentFile = new File(path).getParentFile();
                    if (parentFile == null)
                        continue;
                    String dirPath = parentFile.getAbsolutePath();
                    ImageFloder imageFloder = null;
                    //     HashSet            (      ,            ~~                    if (mDirPaths.contains(dirPath)) {
                        continue;
                    } else {
                        mDirPaths.add(dirPath);
                        //    imageFloder
                        imageFloder = new ImageFloder();
                        imageFloder.setDir(dirPath);
                        imageFloder.setFirstImagePath(path);
                    }
                    int picSize = 0;
                    try{
                        picSize = parentFile.list(new FilenameFilter() {
                            @Override
                            public boolean accept(File dir, String filename) {
                                if (filename.endsWith(".jpg")
                                        || filename.endsWith(".png")
                                        || filename.endsWith(".jpeg"))
                                    return true;
                                return false;
                            }
                        }).length;
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    totalCount += picSize;
                    imageFloder.setCount(picSize);
                    mImageFloders.add(imageFloder);
                    if (picSize > mPicsSize) {
                        mPicsSize = picSize;
                        mImgDir = parentFile;
                    }
                }
                mCursor.close();
                // HashSet         
                mDirPaths = null;
                //   Handler      
                mHandler.sendEmptyMessage(0x110);

            }
        }).start();
    }

    /**
     *    View
     */
    private void initView() {
        mGirdView = (GridView) findViewById(R.id.id_gridView);
        tv_samplecamera_confirm = (TextView) findViewById(R.id.tv_samplecamera_confirm);
        mChooseDir = (TextView) findViewById(R.id.id_choose_dir);
        tv_return = (TextView) findViewById(R.id.tv_return_samplecamera);
        mImageCount = (TextView) findViewById(R.id.id_total_count);
        mBottomLy = (RelativeLayout) findViewById(R.id.id_bottom_ly);
    }

    private void initEvent() {
        tv_samplecamera_confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ArrayList imageList = mAdapter.getImageList();
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("images", imageList);
                intent.putExtras(bundle);
                setResult(11, intent);
                finish();
            }
        });
        tv_return.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        /**
         * popupWindow
         */
        mBottomLy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mListImageDirPopupWindow
                        .setAnimationStyle(R.style.anim_popup_dir);
                mListImageDirPopupWindow.showAsDropDown(mBottomLy, 0, 0);
                //         
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = .3f;
                getWindow().setAttributes(lp);
            }
        });
    }

    public void selected(ImageFloder floder) {
        mImgDir = new File(floder.getDir());
        mImgs = Arrays.asList(mImgDir.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {
                if (filename.endsWith(".jpg") || filename.endsWith(".png")
                        || filename.endsWith(".jpeg"))
                    return true;
                return false;
            }
        }));
        /**
         *                     ,           ;
         */
        mAdapter = new MyAdapter(getApplicationContext(), mImgs,
                R.layout.view_cameragrid_item, mImgDir.getAbsolutePath());
        mGirdView.setAdapter(mAdapter);
        mImageCount.setText(floder.getCount() + " ");
        mChooseDir.setText(floder.getName());
        mListImageDirPopupWindow.dismiss();
    }
}

呼び出し後の効果を見てみましょう
アルバム呼び出し後に返されるのは、写真を含むローカルパス文字列です.
ArrayList imageArrayList = bundle.getStringArrayList("images");

ソースのダウンロード:
http://download.csdn.net/download/jacky_can/9878331