I am working on Android application in which I want to pass image from gallery. Previously my code take image from drawable and send it to cropping class. Now I want to use my gallery pic which I have selected instead of R.id.cropImg.
My code for gallery image with URI is also given below:
// for image gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
// OnActivity
if (requestCode == SELECT_PICTURE) {
final Uri selectedImageUri = data.getData();
//showLoadingDialog();
if (Build.VERSION.SDK_INT < 19) {
selectedImagePath = getPath(selectedImageUri);
int index=1;
mIntent.putExtra("index", index);
startActivityForResult(mIntent, requestCode);
}
}
// now I want to give the location of my image in place
// of R.id.cropImg
setContentView(R.layout.fragment_cropimage);
final CropImageView mCropImage = (CropImageView)findViewById(R.id.cropImg);
mCropImage.setDrawable(getResources().getDrawable(R.drawable.precrop),300,300);
// Before galley it was taking img from drawable for
// cropping, now I want from gallery.
Related
now file:// uri are not supported. people say to use FileProvider but i think i have to save image in app data location to use this. is there a way to get content:// URI Without Saving image in data directory.i want to open image in sd card from gallery. this below code works on old phones, after android 7 it is not working. FileUriExposedException example image file is in dcim folder my old code is this
File pic2toview = new File(imagepath);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(pic2toview), "image/*");
startActivity(i);
Code to select an image from gallery by intent,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE_REQUEST);
Then it handled in onActivityResult,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
imageUrl = data.getData();
}
}
Here imageUrl is Uri of image selected from gallery.
I am using Android Intent Chooser to select a photo from gallery with following code.
ivAvatar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Bir fotoğraf seçin ..."), 1);
}
});
After selection using the path I fill the imageview with following code :
Uri selectedImageUri = data.getData();
imagepath = ImagePathUtil.getPath(getApplicationContext(), selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(imagepath);
ivAvatar.setImageBitmap(bitmap);
selectedU = selectedImageUri;
File f = new File(String.valueOf(selectedU));
if(f.exists())
{
int i = 1;
}
Image can be viewed without any problems, but the File object I create afterwards File 's exists() method always return false.
It is returning false because the file will be null. Please look at the below post for more details on how to get the real path from URI.
URI from Intent.ACTION_GET_CONTENT into File
I am trying to pick an image from the Android Gallery only, not other applications like Photos, file manager etc
I need a solution to open the Gallery App directly, or is it possible to use the Photos Application to pick image?
1) Choose from gallery
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent,CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
2) onActivityResult result code
try {
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger images
options.inSampleSize = 2;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);
descimage.setImageBitmap(bitmap);
bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(new File(fileUri.getPath())));
photostatus = 1;
pbar.setVisibility(View.VISIBLE);
txtbrowser.setEnabled(false);
new upload().execute();
} catch (NullPointerException e) {
e.printStackTrace();
}
You should be aware that Gallery no longer exists on some devices running Lollipop. The photos app is the replacement and it should have no problems handling the intent to select an image. Intent.ACTION_GET_CONTENT is usually recommended for selecting images, such as:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, ID);
Opening the gallery on devices that do have it installed is discussed here. Basically every different vendor may ship a different gallery app.
It is possible to launch a specific activity for an implicit intent (such as selecting an image) without showing the chooser dialog by using the PackageManager.queryIntentActivities() API to iterate all the available packages on the users device so you can explicitly launch the one you require.
This intent allows you to pick the image from the default gallery.
// in onCreate or any event where your want the user to select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
For receiving the selected image in onActivityResult()
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
i got the solution from here
Getting an image from the Gallery with
Intent intentGallery = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intentGallery, CHOOSE_PICTURE);
and
Uri targetUri = data.getData();
Log.v(LOG_TAG,"Data achieved from gallery: "+ targetUri);
results in content://com.android.sec.gallery3d.provider/picasa/ if the user selected a Picasa image.
This solution is not good for me, because i don't want to use internet.
So, there is a way to pick only the local picasa image thumbnail from gallery?
I have a application where I have some downloaded images in a folder in SD Card. I want to save it as a wallpaper.
using the below code user can set it as wallpaper.
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(context);
myWallpaperManager.setBitmap(loadedImage);
However this does not bring up any UI for user to select a part of the image like crop operation when selecting a image from Gallery app to set wallpaper. I would like my code to trigger such a operation. When users click a button in my app I want to take them to gallery app with crop option to set the wallpaper.
Please let me know how to do this. Thank you.
You may want to try this:
To select from your library (SD Card included) - void selectPhoto():
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose photo to upload"), PICK_FROM_FILE);
To start the crop action - void doCrop():
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
// Check if there is image cropper application installed.
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
// If no cropper application is found, throw a message.
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
// If there're cropper applications found, use the first
} else {
// Specify image path and cropping parameters
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 0);
intent.putExtra("outputY", 0);
intent.putExtra("return-data", true);
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROPPED_IMAGE);
Handle Activity results - void onActivityResult(int requestCode, int resultCode, Intent data)
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROPPED_IMAGE:
Bundle extras = data.getExtras();
try{
if (extras != null) {
Bitmap myImage = extras.getParcelable("data");
}
}
catch(Exception e)
{
e.printStackTrace();
}
break;
This code will activate crop action right after you selected the image.
Note that mImageCaptureUri is the selected image URI, it would be pass to intent of cropping action.