我想从画廊创建一个图片选择器。我使用代码
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TFRequestCodes.GALLERY);
我的问题是在这个活动和视频文件显示。是否有一种方法可以过滤显示的文件,以便在此活动中不显示视频文件?
我想从画廊创建一个图片选择器。我使用代码
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TFRequestCodes.GALLERY);
我的问题是在这个活动和视频文件显示。是否有一种方法可以过滤显示的文件,以便在此活动中不显示视频文件?
当前回答
OnActivityResult方法已弃用
val singleImageResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes
val data: Intent? = result.data
val selectedImageUri: Uri? = data?.data
if (null != selectedImageUri) {
// Get the path from the Uri
val path = getPathFromURI(selectedImageUri)
findViewById<TextView>(R.id.textView).text = path
findViewById<ImageView>(R.id.imageView2).setImageURI(selectedImageUri)
}
}
}
findViewById<Button>(R.id.oneImageSelectBtn).setOnClickListener {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
singleImageResultLauncher.launch(Intent.createChooser(intent, "Select Picture"))
}
private fun getPathFromURI(uri: Uri?): String {
var path = ""
if (contentResolver != null) {
val cursor = contentResolver.query(uri!!, null, null, null, null)
if (cursor != null) {
cursor.moveToFirst()
val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME)
path = cursor.getString(idx)
cursor.close()
}
}
return path
}
其他回答
绝对的。试试这个:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
不要忘记创建常量PICK_IMAGE,这样当用户从图库中返回时,你就可以识别出来:
public static final int PICK_IMAGE = 1;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE) {
//TODO: action
}
}
这就是我对图片库的称呼。把它放进去,看看是否对你有用。
编辑:
这会打开Documents应用程序。允许用户使用他们可能已经安装的任何图库应用程序:
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);
public void FromCamera() {
Log.i("camera", "startCameraActivity()");
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 1);
}
public void FromCard() {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(picturePath);
image.setImageBitmap(bitmap);
if (bitmap != null) {
ImageView rotate = (ImageView) findViewById(R.id.rotate);
}
} else {
Log.i("SonaSys", "resultCode: " + resultCode);
switch (resultCode) {
case 0:
Log.i("SonaSys", "User cancelled");
break;
case -1:
onPhotoTaken();
break;
}
}
}
protected void onPhotoTaken() {
// Log message
Log.i("SonaSys", "onPhotoTaken");
taken = true;
imgCapFlag = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(path, options);
image.setImageBitmap(bitmap);
}
您可以使用此方法从图库中选择图像。只显示图像。
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);
}
并重写onActivityResult as
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == 1) {
final Bundle extras = data.getExtras();
if (extras != null) {
//Get image
Bitmap newProfilePic = extras.getParcelable("data");
}
}
}
对于Kotlin使用新的ActivityResultContracts,因为startActivityForResult是废弃的:
private val mSelectedPicDataResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val selectedPicUri = result.data?.data
//use your selected pic
}
}
private fun startSelectPic() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
mSelectedPicDataResult.launch(intent)
}
感谢mklkj。
这是一个活动版本。
fileChooserContract可以选择一个图像。 filesChooserContract可以选择多张图片。
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/*")
}
}
}