PickImage from phone gallery

Get one photo

                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                //String[] mimeTypes = {"image/jpeg","image/png"};
                //intent.putExtra(Intent.EXTRA_MIME_TYPES,mimeTypes);
                startActivityForResult(intent,REQUEST_READ_IMG);

Override onActivityResult to get image

data is Intent , getData() is uri

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_READ_IMG)
        {
            if (resultCode == RESULT_OK){
                imageView.setImageURI(data.getData());
                Toast.makeText(this,"okok",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this,"Fail"+resultCode,Toast.LENGTH_SHORT).show();
            }
        }
    }

Get Multiple Image

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                startActivityForResult(intent,REQUEST_READ_IMG);

ActivityResult

use ClipData Object to get Multiple image

getItemAt.getUri

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_READ_IMG)
        {
            if (resultCode == RESULT_OK){
                ClipData clipData = data.getClipData();
                Log.d(TAG, "onActivityResult: "+clipData.getItemCount());
                for (int i = 0 ; i < clipData.getItemCount() ; i++){
                    Log.d(TAG, "onActivityResult: urls"+clipData.getItemAt(i).getUri());
                }
                imageView.setImageURI(data.getData());
                Toast.makeText(this,"okok",Toast.LENGTH_SHORT).show();
            }else{

                Toast.makeText(this,"Fail"+resultCode,Toast.LENGTH_SHORT).show();
            }
        }
    }

最後修改日期: 2019-04-30 18:56:28

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料