Last active
June 10, 2021 09:24
-
-
Save aotian16/2c9ba428340e8db03a1f38f7c062fc95 to your computer and use it in GitHub Desktop.
使用系统自带的相册选择器选择单或多张照片
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// registerForActivityResult | |
class MainActivity : AppCompatActivity() { | |
companion object { | |
private const val TAG = "MainActivity" | |
} | |
lateinit var viewBinding: ActivityMainBinding | |
private val fileChooserContract = registerForActivityResult(ActivityResultContracts.GetContent()) { uri -> | |
Log.d(TAG, "onActivityResult: uri $uri") | |
} | |
private val filesChooserContract = registerForActivityResult(ActivityResultContracts.GetMultipleContents()) { uriList -> | |
for (uri in uriList) { | |
Log.d(TAG, "onActivityResult: uri $uri") | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
viewBinding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(viewBinding.root) | |
viewBinding.btnPick.setOnClickListener { | |
fileChooserContract.launch("image/*") | |
} | |
viewBinding.btnPickMulti.setOnClickListener { | |
filesChooserContract.launch("image/*") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// startActivityForResult | |
class MainActivity : AppCompatActivity() { | |
companion object { | |
private const val TAG = "MainActivity" | |
const val REQUEST_CODE_PICK_IMAGE = 1000 | |
const val REQUEST_CODE_PICK_IMAGES = 1010 | |
} | |
lateinit var viewBinding: ActivityMainBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
viewBinding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(viewBinding.root) | |
viewBinding.btnPick.setOnClickListener { | |
val intent = Intent(Intent.ACTION_GET_CONTENT) | |
intent.type = "image/*" | |
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true) | |
val chooserIntent = Intent.createChooser(intent, "Select Picture") | |
startActivityForResult(chooserIntent, REQUEST_CODE_PICK_IMAGE) | |
} | |
viewBinding.btnPickMulti.setOnClickListener { | |
val intent = Intent(Intent.ACTION_GET_CONTENT) | |
intent.type = "image/*" | |
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) | |
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true) | |
val chooserIntent = Intent.createChooser(intent, "Select Picture") | |
startActivityForResult(chooserIntent, REQUEST_CODE_PICK_IMAGES) | |
} | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if (requestCode == REQUEST_CODE_PICK_IMAGE) { | |
if (resultCode == RESULT_OK) { | |
data?.data?.let { | |
Log.d(TAG, "onActivityResult: uri $it") | |
} | |
} | |
} else if (requestCode == REQUEST_CODE_PICK_IMAGES) { | |
if (resultCode == RESULT_OK) { | |
data?.data?.let { | |
Log.d(TAG, "onActivityResult: uri $it") | |
} | |
data?.clipData?.let { clipData -> | |
for (index in 0 until clipData.itemCount) { | |
val item = clipData.getItemAt(index) | |
Log.d(TAG, "onActivityResult: index $index uri ${item.uri}") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment