我使用以下代码直接打开Gallery应用程序以从库中选择图像。但它提供了另一个选项,如屏幕截图所示。
因此可以直接打开Gallery应用程序并显示任何选项。我使用的代码如下所示。
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);
答案 0 :(得分:2)
以下是按照以下步骤解决问题的方法。
<强> Layout.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" />
答案 1 :(得分:0)
试试这个
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Complete action using"), SELECT_IMAGE);
答案 2 :(得分:0)
当打开另一个应用程序的意图被触发时,android将显示可以处理此意图的应用程序。它不是可以从应用程序代码修复的东西。解决方案是将应用程序设置为该特定操作的默认值。
答案 3 :(得分:0)
您的问题的答案是自定义网格视图选择。
我找到了一个很好的示例,可让您将图库中的所有图像显示到您的活动中。这是不同的。因为这不会打开其他应用程序。您可以对此代码进行自定义。
样本名称为 AndroidGridViewCompatLib
要实现此代码,您需要在布局文件中编写以下代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/doneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:enabled="false"
android:text="Done" />
<com.rokoder.android.lib.support.v4.widget.GridViewCompat
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/doneButton"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:choiceMode="multipleChoice"
android:columnWidth="120dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</com.rokoder.android.lib.support.v4.widget.GridViewCompat>
</RelativeLayout>
以下代码将显示在您的活动文件中。
@Override
protected void onCreate(Bundle saveInstance) {
...
...
gridView = (GridViewCompat) findViewById(R.id.gridView);
gridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
gridView.setAdapter(imageAdapter);
...
...
}
此示例为CHOICE_MODE_MULTIPLE
,您也可以将其设置为CHOICE_MODE_SINGLE
。
答案 4 :(得分:0)
button_pick_image.setOnClickListener(view -> start());
final Activity activity = Main2Activity.this;
final String[] permissions = new String[]{Manifest.permission.CAMERA};
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, permissions, RC_CAMERA);
} else {
captureImage();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == RC_CAMERA) {
if (grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
captureImage();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
private void captureImage() {
ImagePicker.cameraOnly().start(Main2Activity.this,RC_IMAGE_PICKER);
}
Action1<List<com.esafirm.imagepicker.model.Image>> action = this::printImages;
private rx.Observable<List<com.esafirm.imagepicker.model.Image>> getImagePickerObservable() {
return RxImagePicker.getInstance()
.start(this, ImagePicker.create(this));
}
public void start() {
ImagePicker imagePicker = ImagePicker.create(this)
.toolbarArrowColor(Color.RED) // set toolbar arrow up color
.toolbarFolderTitle("Folder") // folder selection title
.toolbarImageTitle("Tap to select"); // image selection title
imagePicker.multi(); // multi mode (default mode)
imagePicker.limit(10) // max images can be selected (99 by default)
.showCamera(true) // show camera or not (true by default)
.imageDirectory("Camera") // captured image directory name ("Camera" folder by default)
.imageFullDirectory(Environment.getExternalStorageDirectory().getPath()) // can be full path
.start(); // start image picker activity with request code
}
@Override
protected void onActivityResult(int requestCode, final int resultCode, Intent data) {
if (ImagePicker.shouldHandle(requestCode, resultCode, data)) {
images = (ArrayList<com.esafirm.imagepicker.model.Image>) ImagePicker.getImages(data);
printImages(images);
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void printImages(List<com.esafirm.imagepicker.model.Image> images) {
if (images == null) return;
StringBuilder stringBuffer = new StringBuilder();
for (int i = 0, l = images.size(); i < l; i++) {
stringBuffer.append(images.get(i).getPath()).append("\n");
}
textView.setText(stringBuffer.toString());
}
}