gpt4 book ai didi

android - 如何直接打开相册应用选择图片

转载 作者:行者123 更新时间:2023-11-29 00:25:30 25 4
gpt4 key购买 nike

我正在使用以下代码直接打开 Gallery 应用程序以从图库中选取图像。但它提供了另一个选项,如屏幕截图所示。

enter image description here

那么是否可以不显示任何选项直接打开图库应用程序?我使用的代码如下所示。

Intent imageintent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imageintent.setType("image/*");
imageintent.putExtra("crop", "true");
imageintent.putExtra("aspectX", 1);
imageintent.putExtra("aspectY", 1);
imageintent.putExtra("return-data", true);
startActivityForResult(imageintent, SELECT_IMAGE);

最佳答案

这里是您的问题的解决方案,只需按照以下步骤操作即可。

布局.xml

 <ImageView
android:id="@+id/circleImage"
android:layout_width="@dimen/d109dp"
android:layout_height="@dimen/d109dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/d10dp"
android:src="@drawable/man"
app:civ_border_color="@android:color/white"
app:civ_border_width="@dimen/d2dp" />

MainActivity.java

 case R.id.imageButtonEdit:
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};

AlertDialog.Builder builder = new AlertDialog.Builder(EditProfileActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result = Utility.checkPermission(EditProfileActivity.this);

if (items[item].equals("Take Photo")) {
userChoosenTask = "Take Photo";
if (result)
cameraIntent();

} else if (items[item].equals("Choose from Library")) {
userChoosenTask = "Choose from Library";
if (result)
galleryIntent();

} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();

private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}


private void galleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}

if (requestCode == PLACE_PICKER_REQUEST && resultCode == Activity.RESULT_OK) {
hideProgressDialog();
final Place place = PlacePicker.getPlace(getApplicationContext(), data);
// final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}

editLocation.setText(MessageFormat.format("{0}{1}{2}", String.valueOf(address), getString(R.string.textComma), Html.fromHtml(attributions)));


}
}

private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");

FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

ivImage.setImageBitmap(thumbnail);
}

private void onSelectFromGalleryResult(Intent data) {
Bitmap bm = null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}

ivImage.setImageBitmap(bm);
}

在 Manifest.xml 文件中设置使用权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

关于android - 如何直接打开相册应用选择图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19930131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com