在我的应用程序中,我试图仅打开默认的内置图库应用程序,甚至不打开照片应用程序和其他文件资源管理器应用程序。点击按钮,它将直接登陆图库,我该怎么做?
我的代码 ' Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("图像/ *&#34); startActivityForResult(意向,PICK_IMAGE);'
答案 0 :(得分:0)
试试这个...
private int PICK_IMAGE_REQUEST = 1;
tvGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
在 Android清单文件
中使用权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
尝试这样
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE)
{
if (resultCode == Activity.RESULT_OK)
{
if (data != null)
{
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
} catch (IOException e)
{
e.printStackTrace();
}
}
} else if (resultCode == Activity.RESULT_CANCELED)
{
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_SHORT).show();
}
} }
获取图片的OnActivityResult
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在清单文件中添加权限
row features
1 'a | b | c'
2 'a | c'
3 'b | c | d'
4 'a'
答案 2 :(得分:0)
试试这个: -
public static final int GALLERY_PICTURE = 1;
private String selectedImagePath = null;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), GALLERY_PICTURE);
onActivityResult() :-
if (requestCode == GALLERY_PICTURE && resultCode == RESULT_OK) {
selectedImagePath = getRealPathFromURI_API19(this, data.getData());
Log.e("gallery path", selectedImagePath);
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
String id = wholeID.split(":")[1];
String[] column = {MediaStore.Images.Media.DATA};
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}