Java 从Android Intent打开gallery应用程序
我正在寻找一种方法来打开Java 从Android Intent打开gallery应用程序,java,android,android-intent,Java,Android,Android Intent,我正在寻找一种方法来打开Androidgallery应用程序 我不想返回图片,只想打开图库,让用户使用它,就像他们从启动器中选择它一样(查看图片/文件夹) 我已尝试做以下工作: Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.setFlags(Intent.FLAG_ACTIVITY_NE
Android
gallery应用程序
我不想返回图片,只想打开图库,让用户使用它,就像他们从启动器中选择它一样(查看图片/文件夹
)
我已尝试做以下工作:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
但是,这会导致在我选择图片后关闭应用程序(我知道这是因为操作\u GET\u CONTENT
),但我只需要打开图库
任何帮助都会很好
谢谢您可以使用以下意图打开图库:
public static final int RESULT_GALLERY = 0;
Intent galleryIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );
如果要获取结果URI或执行任何其他操作,请使用以下命令:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case QuestionEntryView.RESULT_GALLERY :
if (null != data) {
imageUri = data.getData();
//Do whatever that you desire here. or leave this blank
}
break;
default:
break;
}
}
在安卓4.0+上测试由于您不想返回结果,请尝试以下简单代码
Intent i=new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivity(i);
我希望这对你有帮助。这个代码对我有用
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
ResultCode用于将所选图像更新到库视图
if(requestCode==RESULT\u LOAD\u IMAGE&&resultCode==RESULT\u OK
&&空!=数据){
Uri selectedImage=data.getData();
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
Cursor Cursor=getContentResolver().query(selectedImage,
filePathColumn,null,null,null);
if(cursor==null | | cursor.getCount()<1){
return;//没有光标或记录。请执行错误处理
}
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
if(columnIndex<0)//没有列索引
return;//执行错误处理
String picturePath=cursor.getString(columnIndex);
cursor.close();//关闭游标
photo=解码文件路径(picturePath.toString());
列表位图=新的ArrayList();
位图。添加(照片);
ImageAdapter ImageAdapter=新的ImageAdapter(
AddIncidentScreen.this,位图);
imageAdapter.notifyDataSetChanged();
newTagImage.setAdapter(图像适配器);
}
这就是您需要的:
ACTION_VIEW
将代码更改为:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
private File mCurrentPhoto;
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_IMAGE_SELECTOR:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = App.Instance.getContentResolver().query(data.getData(), filePathColumn, null, null, null);
if (cursor == null || cursor.getCount() < 1) {
mCurrentPhoto = null;
break;
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
if(columnIndex < 0) { // no column index
mCurrentPhoto = null;
break;
}
mCurrentPhoto = new File(cursor.getString(columnIndex));
cursor.close();
} else {
mCurrentPhoto = null;
}
break;
case REQUEST_IMAGE_CAPTURE:
if (resultCode != Activity.RESULT_OK) {
mCurrentPhoto = null;
}
break;
}
if (mCurrentPhoto != null) {
ImageView imageView = (ImageView) [parent].findViewById(R.id.loaded_iv);
Picasso.with(App.Instance).load(mCurrentPhoto).into(imageView);
}
super.onActivityResult(requestCode, resultCode, data);
}
@覆盖
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
开关(请求代码){
案例请求\图像\选择器:
if(resultCode==Activity.RESULT\u OK&&data!=null&&data.getData()!=null){
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
Cursor Cursor=App.Instance.getContentResolver().query(data.getData(),filePathColumn,null,null);
if(cursor==null | | cursor.getCount()<1){
mCurrentPhoto=null;
打破
}
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
如果(columnIndex<0){//没有列索引
mCurrentPhoto=null;
打破
}
mCurrentPhoto=新文件(cursor.getString(columnIndex));
cursor.close();
}否则{
mCurrentPhoto=null;
}
打破
案例请求\u图像\u捕获:
if(resultCode!=活动。结果\u确定){
mCurrentPhoto=null;
}
打破
}
如果(mCurrentPhoto!=null){
ImageView=(ImageView)[parent].findViewById(R.id.loaded_iv);
毕加索.with(App.Instance).load(mCurrentPhoto).into(imageView);
}
super.onActivityResult(请求代码、结果代码、数据);
}
如果有人仍然得到错误,即使添加了以下代码
Intent Intent=新Intent();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(Intent.createChooser(Intent,“选择图片”),PICK_IMAGE)代码>这应该可以回答您的问题。。。你的minSdkVersion是什么版本?光标不应该关闭吗?在我的情况下(android 7.0),picturePath为空。无法导入decodeFilePath()。改为使用BitmapFactory.decodeFile(路径名)。这是唯一回答此问题的答案!他只需要我只需要打开画廊。不选择或返回nadaThis这是我一直在寻找的答案,我也在我的一个应用程序中实现了相同的代码,但它在某些设备上崩溃:-(如何使用此意图打开gallary中的图片文件夹,请您帮助我。非常感谢:)打开图库确实需要许可吗。我不需要使用任何权限来打开图库。@IsharaAmarasekera如果没有崩溃,对你来说一切正常,那么就没有了
//A system-based view to select photos.
private void dispatchPhotoSelectionIntent() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
this.startActivityForResult(galleryIntent, REQUEST_IMAGE_SELECTOR);
}
//Open system camera application to capture a photo.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(App.Instance.getPackageManager()) != null) {
try {
createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (mCurrentPhoto != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhoto));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_IMAGE_SELECTOR:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = App.Instance.getContentResolver().query(data.getData(), filePathColumn, null, null, null);
if (cursor == null || cursor.getCount() < 1) {
mCurrentPhoto = null;
break;
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
if(columnIndex < 0) { // no column index
mCurrentPhoto = null;
break;
}
mCurrentPhoto = new File(cursor.getString(columnIndex));
cursor.close();
} else {
mCurrentPhoto = null;
}
break;
case REQUEST_IMAGE_CAPTURE:
if (resultCode != Activity.RESULT_OK) {
mCurrentPhoto = null;
}
break;
}
if (mCurrentPhoto != null) {
ImageView imageView = (ImageView) [parent].findViewById(R.id.loaded_iv);
Picasso.with(App.Instance).load(mCurrentPhoto).into(imageView);
}
super.onActivityResult(requestCode, resultCode, data);
}