Reputation: 4559
I am using the following code to open the Gallery app directly to pick images from the gallery. But it provides another option as shown in the screenshot.
So is it possible to open the Gallery app directly without showing any options? The code I used is shown below.
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);
Upvotes: 2
Views: 8945
Reputation: 581
Here is the solution.
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);
}
Set Uses Permission in Manifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Upvotes: 3
Reputation: 107
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());
}
}
//use this or refer this https://jitpack.io/#esafirm/android-image-picker
Upvotes: 0
Reputation: 26034
Answer of your question is Custom Grid View selection.
I found a good sample which will allow you to display all images from your gallery to your activity. This is different. Because this will not open other application. You can do customization on this code.
Name of sample is AndroidGridViewCompatLib
To implement this code, you need to write following code in your layout file.
<?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>
Following code will be in your activity file.
@Override
protected void onCreate(Bundle saveInstance) {
...
...
gridView = (GridViewCompat) findViewById(R.id.gridView);
gridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
gridView.setAdapter(imageAdapter);
...
...
}
Here example is CHOICE_MODE_MULTIPLE
, you can also set it CHOICE_MODE_SINGLE
.
Upvotes: 0
Reputation: 1171
When the intent to open an another app is fired, the android will show up the applications that can handle this intent. It is not something that can be fixed from the application code. The solution is to set the application as default for that perticular action.
Upvotes: 0
Reputation: 550
Try this
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Complete action using"), SELECT_IMAGE);
Upvotes: 0