我想从图库中创建图片选择器。我使用代码
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);
我的问题是在此活动中显示了视频文件。有没有办法过滤显示的文件,以便在此活动中不显示视频文件?
Answers:
绝对。试试这个:
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
}
}
这就是我所谓的图片库。放入并查看它是否适合您。
编辑:
这将打开“文档”应用程序。要允许用户还使用他们可能已安装的任何图库应用程序:
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);
@Override
函数上进一步使用onActivityResult(int requestCode, resultCode, Intent data)
,建议您requestCode
在执行任何操作之前使用此常量检查参数:)
有时,您无法从所选图片中获取文件。这是因为选择的一个来自Google+,云端硬盘,Dropbox或任何其他提供商。
最好的解决方案是要求系统通过Intent.ACTION_GET_CONTENT选择内容,并通过内容提供程序获取结果。
您可以按照下面的代码或查看我更新的要点。
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}
if(resultCode == Activity.RESULT_OK) {...}
可以用来检测成功/取消
new File(data.getData()).getAbsolutePath()
只是一个猜测,我还没有尝试过
getActivity().getContentResolver().openInputStream(data.getData());
。
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);
}
path
多少?于File file = new File(path);
您可以使用此方法从图库中选择图像。仅显示图像。
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覆盖为
@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");
}
}
}
这是请求权限(如果需要)的完整示例,从图库中选择图片,然后将图片转换为bitmap
或file
AndroidManifesh.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
活动
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button_pick_image.setOnClickListener {
pickImage()
}
}
private fun pickImage() {
if (ActivityCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
val intent = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI
)
intent.type = "image/*"
intent.putExtra("crop", "true")
intent.putExtra("scale", true)
intent.putExtra("aspectX", 16)
intent.putExtra("aspectY", 9)
startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE)
} else {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
READ_EXTERNAL_STORAGE_REQUEST_CODE
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_REQUEST_CODE) {
if (resultCode != Activity.RESULT_OK) {
return
}
val uri = data?.data
if (uri != null) {
val imageFile = uriToImageFile(uri)
// todo do something with file
}
if (uri != null) {
val imageBitmap = uriToBitmap(uri)
// todo do something with bitmap
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
READ_EXTERNAL_STORAGE_REQUEST_CODE -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// pick image after request permission success
pickImage()
}
}
}
}
private fun uriToImageFile(uri: Uri): File? {
val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
val cursor = contentResolver.query(uri, filePathColumn, null, null, null)
if (cursor != null) {
if (cursor.moveToFirst()) {
val columnIndex = cursor.getColumnIndex(filePathColumn[0])
val filePath = cursor.getString(columnIndex)
cursor.close()
return File(filePath)
}
cursor.close()
}
return null
}
private fun uriToBitmap(uri: Uri): Bitmap {
return MediaStore.Images.Media.getBitmap(this.contentResolver, uri)
}
companion object {
const val PICK_IMAGE_REQUEST_CODE = 1000
const val READ_EXTERNAL_STORAGE_REQUEST_CODE = 1001
}
}
如果您只是在寻找图像和多项选择。
看看@一次https://stackoverflow.com/a/15029515/1136023
这对将来很有帮助。我个人使用MultipleImagePick感觉很好。
根据文档,仅向API最低19岁的用户提供答案的更新:
在Android 4.4(API级别19)及更高版本上,您还可以使用ACTION_OPEN_DOCUMENT意向,该附加意向显示系统控制的选择器UI,该用户界面受控制,允许用户浏览其他应用程序提供的所有文件。用户可以通过单个UI从任何受支持的应用中选择文件。
在Android 5.0(API级别21)及更高版本上,您还可以使用ACTION_OPEN_DOCUMENT_TREE意图,该意图允许用户选择客户端应用程序可以访问的目录。
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.type = "image/*"
startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE)
对于仅从本地选择,请添加以下内容:
i.putExtra(Intent.EXTRA_LOCAL_ONLY,true)
这工作很好:
val i = Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
i.type = "image/*"
i.putExtra(Intent.EXTRA_LOCAL_ONLY,true)
startActivityForResult(Intent.createChooser(i,"Select Photo"),pickImageRequestCode)
你可以比这个答案更容易做到:
Uri Selected_Image_Uri = data.getData();
ImageView imageView = (ImageView) findViewById(R.id.loadedimg);
imageView.setImageURI(Selected_Image_Uri);