On click, my app gives choice between camera and gallery and that picture is then displayed in an ImageView. I originally tried to display the full image and then tried to use the bitmap way but nothing works. I just get a blank ImageView. Please give me some guidance as to what I'm doing wrong and ask for clarifications if necessary:
Camera/gallery photo code:
Uri outputFileUri;
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent) {
super.onActivityResult(resultCode, requestCode, returnIntent);
if(requestCode == 0) {
if(resultCode == RESULT_OK) {
final boolean isCamera;
if(returnIntent == null) {
isCamera = true;
}
else
{
final String action = returnIntent.getAction();
if(action == null) {
isCamera = false;
}
else {
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if(isCamera) {
selectedImageUri = outputFileUri;
mainImage.setImageURI(selectedImageUri); //trying full image
}
else {
selectedImageUri = returnIntent == null ? null : returnIntent.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
mainImage.setImageBitmap(bitmap); //trying bitmap
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
your code is 2000000000000000% ok i test it myself
Your problem is your ImageView can't show image because of image size. I try this code with ImageView like this
<ImageView
android:id="#+id/mainImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/abc_ab_bottom_solid_dark_holo" />
If you use height and width with dp like this
android:layout_width="100dp"
android:layout_height="100dp"
You need to compress the Bitmap to show it in ImageView.
Edit your code to conversion
if(isCamera) {
selectedImageUri = outputFileUri;
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);//You can use this bitmap if need full image to further use
Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, 600 ,600, true);//this bitmap2 you can use only for display
mainImage.setImageBitmap(bitmap2); //trying full image
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
selectedImageUri = returnIntent == null ? null : returnIntent.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
bitmap = Bitmap.createScaledBitmap(bitmap, 600 ,600, true);
mainImage.setImageBitmap(bitmap); //trying bitmap
} catch (IOException e) {
e.printStackTrace();
}
}
If you want to take image from camera you can go with this process:
public void fromCamera(){
String path= Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
File file = new File(path,"IMG_"+System.currentTimeMillis()+".jpg");
file.getParentFile().mkdirs();
try {
file.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
mPicCaptureUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPicCaptureUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
And if you want image from gallery then :
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_file)), REQUEST_GALLERY);
On your onActivityResult you can get the image path of selected one and set image in image view....
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if (requestCode == REQUEST_GALLERY && data != null && data.getData() != null) {
Uri selectedImageUri = data.getData();
//do set your image here
}else if(requestCode == REQUEST_CAMERA){
if(mPicCaptureUri!=null){
//do try to set image here
}
}
}
}
don't forgrt to define mPicCaptureUri at the top as:
private Uri mPicCaptureUri = null;
You can take idea from above code ..it might help you
Now you could use Picasso Library :D
Picasso.with(context)
.load(item.getPhotoUri())
.resize(512,512)
.placeholder(R.drawable.noimage)
.error(R.drawable.error_image)
.into(photo);
This mostly happens due to image being too big to be rendered by bitmap within that span of nano second during runtime, so you won't see it. Use a library called Glide. It solved all my image issues in Android including performance when there are hundreds of images to be displayed in a single list or grid.
Related
Hi basically I am selecting image form gallery or capture from camera and cropping it. If I crop image which is selected from gallery is no blur its fine but if I crop captured images means its getting blur.
To start camera I have used
private void cameraIntent()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
and in camera onActivityResult
private void onCaptureImageResult(Intent data) {
Bitmap bm = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
crop(getImageUri(csActivity,bm));
}
This is for crop image
private void crop(Uri uri)
{
final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(uri);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CROP);
}
and in crop result I have used
private void onCropImg(Intent data)
{
Bitmap bm = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 75, stream);
Uri tempUri = getImageUri(csActivity,bm);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File destination = new File(getRealPathFromURI(tempUri));
csProfileImg.setImageBitmap(bm);
uploadProfileImg(destination);
}
and to getImageUri
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
Please help me
You can use a library to crop image, that would give proper results along with the image quality. use: compile 'com.theartofdev.edmodo:android-image-cropper:2.3.+'
And for more you can visit this page: check my answer
This answer will helps others to resolve issue on capture photo from camera which gives a blur image while cropping.
By adding library in build.gradle :
implementation 'com.theartofdev.edmodo:android-image-cropper:2.3.+'
Adding Permission on Manifest :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
CameraActivity.java :
public class CameraActivity extends Activity {
private CropImageView mCropImageView;
private CircleImageView imVCature_pic;
private Uri mCropImageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mCropImageView = (CropImageView) findViewById(R.id.CropImageView);
imVCature_pic = (CircleImageView) findViewById(R.id.imVCature_pic);
}
/**
* On load image button click, start pick image chooser activity.
*/
public void onLoadImageClick(View view) {
startActivityForResult(getPickImageChooserIntent(), 200);
}
/**
* Crop the image and set it back to the cropping view.
*/
public void onCropImageClick(View view) {
Bitmap cropped = mCropImageView.getCroppedImage(500, 500);
if (cropped != null)
imVCature_pic.setImageBitmap(cropped);
//mCropImageView.setImageBitmap(cropped);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri imageUri = getPickImageResultUri(data);
// For API >= 23 we need to check specifically that we have permissions to read external storage,
// but we don't know if we need to for the URI so the simplest is to try open the stream and see if we get error.
boolean requirePermissions = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
isUriRequiresPermissions(imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
requirePermissions = true;
mCropImageUri = imageUri;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
if (!requirePermissions) {
mCropImageView.setImageUriAsync(imageUri);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mCropImageView.setImageUriAsync(mCropImageUri);
} else {
Toast.makeText(CameraActivity.this, "Required permissions are not granted", Toast.LENGTH_LONG).show();
}
}
/**
* Create a chooser intent to select the source to get image from.<br/>
* The source can be camera's (ACTION_IMAGE_CAPTURE) or gallery's (ACTION_GET_CONTENT).<br/>
* All possible sources are added to the intent chooser.
*/
public Intent getPickImageChooserIntent() {
// Determine Uri of camera image to save.
Uri outputFileUri = getCaptureImageOutputUri();
List<Intent> allIntents = new ArrayList<>();
PackageManager packageManager = getPackageManager();
// collect all camera intents
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
if (outputFileUri != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
}
allIntents.add(intent);
}
// collect all gallery intents
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
for (ResolveInfo res : listGallery) {
Intent intent = new Intent(galleryIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
allIntents.add(intent);
}
// the main intent is the last in the list (fucking android) so pickup the useless one
Intent mainIntent = allIntents.get(allIntents.size() - 1);
for (Intent intent : allIntents) {
if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
mainIntent = intent;
break;
}
}
allIntents.remove(mainIntent);
// Create a chooser from the main intent
Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");
// Add all other intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));
return chooserIntent;
}
/**
* Get URI to image received from capture by camera.
*/
private Uri getCaptureImageOutputUri() {
Uri outputFileUri = null;
File getImage = getExternalCacheDir();
if (getImage != null) {
outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg"));
}
return outputFileUri;
}
/**
* Get the URI of the selected image from {#link #getPickImageChooserIntent()}.<br/>
* Will return the correct URI for camera and gallery image.
*
* #param data the returned data of the activity result
*/
public Uri getPickImageResultUri(Intent data) {
boolean isCamera = true;
if (data != null && data.getData() != null) {
String action = data.getAction();
isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
return isCamera ? getCaptureImageOutputUri() : data.getData();
}
/**
* Test if we can open the given Android URI to test if permission required error is thrown.<br>
*/
public boolean isUriRequiresPermissions(Uri uri) {
try {
ContentResolver resolver = getContentResolver();
InputStream stream = resolver.openInputStream(uri);
stream.close();
return false;
} catch (FileNotFoundException e) {
if (e.getCause() instanceof Exception) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
activity_camera.xml :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onLoadImageClick"
android:padding="#dimen/activity_horizontal_margin"
android:text="Load Image" />
<com.theartofdev.edmodo.cropper.CropImageView
android:id="#+id/CropImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:scaleType="fitCenter" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onCropImageClick"
android:padding="#dimen/activity_horizontal_margin"
android:text="Crop Image" />
</LinearLayout>
I have followed some examples on SO of how to retrive an image from Camera or Gallary. The camera part works, but the gallary part dosn't. The code seems very diffuclt to understand for me, so I dont know what exactly to look after.
I also have the needed permissions in my manifest.
Here is a video of the problem: https://www.youtube.com/watch?v=OOoY1y4W86w
ImagePicker(View V), intent/choosers, files, URIS
public void ImagePicker(View v) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
final File rootdir = new File(Environment.getExternalStorageDirectory() + File.separator + "TravelDiary" + File.separator);
rootdir.mkdirs();
final String filename = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirecotry = new File(rootdir, filename);
outputFileUri = Uri.fromFile(sdImageMainDirecotry);
Log.d("TAG", "IM HERE 1");
//camera
final List<Intent> cameraIntents = new ArrayList<>();
final Intent CameraCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listcam = packageManager.queryIntentActivities(CameraCaptureIntent, 0);
Log.d("TAG", "IM HERE 2");
for (ResolveInfo res : listcam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(CameraCaptureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
Log.d("TAG", "IM HERE 3");
}
//Gallary
final Intent imageChooser = new Intent();
imageChooser.setType("image/*");
imageChooser.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(imageChooser, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, SELECT_FROM_GALLARY);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
} else {
Toast.makeText(this, "External storage not available", Toast.LENGTH_SHORT).show();
}
}
onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_FROM_GALLARY) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(selectedImageUri.getPath(), options);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
pic.setBackground(drawable);
} else {
selectedImageUri = data == null ? null : data.getData();
Log.d("ImageURI", selectedImageUri.getLastPathSegment());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
try {
InputStream input = getContentResolver().openInputStream(selectedImageUri);
final Bitmap bitmap = BitmapFactory.decodeStream(input, null, options);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
pic.setBackground(drawable);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
Use below code to pick image from Gallery.
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
//Here PICK_FROM_GALLERY is a requestCode
startActivityForResult(intent, PICK_FROM_GALLERY);
In onActivityResult():
if (resultCode == Activity.RESULT_OK && requestCode == PICK_FROM_GALLERY) {
if (data.getData() != null) {
mImageUri = data.getData();
} else {
//showing toast when unable to capture the image
Debug.toastValid(context, "Unable to upload Image Please Try again ...");
}
}
Problem: The user may take/select up to 3 photos. I'm having trouble in figuring out how to fill the 3 cases; I'm not sure how I could do to retrieve the corresponding ImageView ID.
I tried the putextra since I'm using
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE), but it seems that it's not possible to use the putextra method (I don't retrieve any extra)
So let me share the code with you and feel free to let me know if you would proceed differently. Thanks a lot!
So here I'm catching the click event and passing the V.getID to the method that will handle the actions related to selecting/taking photos.
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.add_item_give_button:
checkAddedItem();
break;
case R.id.add_item_image_1:
selectImage(v.getId());
break;
case R.id.add_item_image_2:
selectImage(v.getId());
break;
case R.id.add_item_image_3:
selectImage(v.getId());
break;
}
}
The selectImage method is called and will handle the alertDialog that will ask if the user wants either to take a picture or to select one. I'm trying to pass the ID in the putExtra method, but nothing is received in the startActivityForResult
public void selectImage(final int imageViewID){
final CharSequence[] options = {getString(R.string.cameral_select_photo_label), getString(R.string.camera_take_photo_label), getString(R.string.common_cancel_label)};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getString(R.string.camera_dialog_title_label));
builder.setItems(options, new DialogInterface.OnClickListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
#Override
public void onClick(DialogInterface dialog, int which) {
if(options[which].equals(getString(R.string.camera_take_photo_label))){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("ImageViewID", imageViewID);
startActivityForResult(intent, REQUEST_CAMERA);
}
else if(options[which].equals(getString(R.string.cameral_select_photo_label))){
Utils.verifyStoragePermissions(getActivity());
Intent intent = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, getResources().getText(R.string.camera_select_image)),SELECT_FILE);
}
else if(options[which].equals(getString(R.string.common_cancel_label))){
dialog.dismiss();
}
}
});
builder.show();
}
In the startActivityForResult, I don't receive the ImageViewID. So for now, I'm just putting the image in the first ImageView since I'm not able to retrieve the right ID.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK){
if(requestCode == REQUEST_CAMERA){
Log.d("Data content", String.valueOf(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 (IOException e) {
e.printStackTrace();
}
itemPic1.setImageBitmap(thumbnail);
} else if (requestCode == SELECT_FILE){
Log.d("imageViewOrigin", String.valueOf(data.getIntExtra("imageViewID", 0)));
Uri selectedImageUrl = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(getContext(), selectedImageUrl, projection, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while(options.outWidth / scale / 2 >= REQUIRED_SIZE && options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale += 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
itemPic1.setImageBitmap(bm);
}
}
}
I would recommend setting tags on the ImageViews. Follow the link, its a similar issue What is the main purpose of setTag() getTag() methods of View?. Let me know if you need more help!
Try this way :
private void openImageIntent(int IMAGE_TYPE) {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "mycapturedImage" + File.separator);
root.mkdirs();
final String fname = getUniqueImageFilename();
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Choisir une Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, IMAGE_TYPE);
}
Then retrieve each Image like this :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
if (resultCode == RESULT_OK) {
if (requestCode == FIRST_IMAGE_INTENT) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
if (isCamera) {
selectedCaptureUri = outputFileUri;
} else {
selectedCaptureUri = data == null ? null : data.getData();
}
//Display image here
} else if (requestCode == SECOND_PICTURE_INTENT) {...}
I tried to crop an image from a Uri after taking a photo or picking a picture. And my codes are like these:
public static void cropImage(Uri uri, Activity activity, int action_code) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 600);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(intent, action_code);
} else {
Toast.makeText(activity, "No Crop App Available", Toast.LENGTH_SHORT).show();
}
}
And overridding onActivityResult() like this:
if (resultCode == Activity.RESULT_OK && requestCode == Utils.CODE_CROP_IMAGE) {
Bundle extras = data.getExtras();
showCenterToast("ccc");
if (extras != null) {
showCenterToast("CCC");
Bitmap photo = extras.getParcelable("data");
ivAvatar.setImageBitmap(photo); // display image in ImageView
FileOutputStream fos = null;
try {
fos = new FileOutputStream(Utils.AVATAR_FILE);
photo.compress(Bitmap.CompressFormat.PNG, 100, fos);// (0-100)compressing file
showCenterToast("DDD");
Utils.AVATAR_FILE_TMP.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
IoUtil.closeSilently(fos);
}
}
}
On devices in Android Pre-Lollipop, I was able to obtain Bitmap photo and display it in an ImageView. But, on Android Lollipop, I always got null from data.getExtras();.
I googled a lot but got few useful things about cropping an image on Android Lollipop.
Android changed its returning mechanism of cropping of com.android.camera.action.CROP on Lollipop. So, what is the new mechanism? How could I get the returned Bitmap after cropping on Lollipop?
Any tips will be appreciated. Thanks in advance.
I think that your problem has nothing to do with Android version but the image you want to be cropped. Cropping image processed in class com.android.gallery3d.filtershow.crop.CropActivity#BitmapIOTask. When the image is too large to return, it will try to return the thumb of the image, and will return null sometimes.
To avoid this, you can get the uri of cropped image instead of bitmap by setting intent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri); where tmpUri is an uri created to hold the result. And then you can get the bitmap from tmpUri.
Sample code:
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file
private static Uri tmpUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap
public static void cropImage(Uri uri, Activity activity, int action_code) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 600);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(intent, action_code);
} else {
Toast.makeText(activity, "No Crop App Available", Toast.LENGTH_SHORT).show();
}
}
And in function onActivityResult:
if (resultCode == Activity.RESULT_OK && requestCode == Utils.CODE_CROP_IMAGE) {
// Bundle extras = data.getExtras();
Uri uri = data.getData();
showCenterToast("ccc");
if (uri != null) {
showCenterToast("CCC");
// Bitmap photo = null;
// if (tmpUri != null) {
// photo = decodeBitmapFromUri(tmpUri); // Get bitmap from uri.
// }
Bitmap photo = decodeUriAsBitmap(uri);
ivAvatar.setImageBitmap(photo); // display image in ImageView
FileOutputStream fos = null;
try {
fos = new FileOutputStream(Utils.AVATAR_FILE);
photo.compress(Bitmap.CompressFormat.PNG, 100, fos);// (0-100)compressing file
showCenterToast("DDD");
Utils.AVATAR_FILE_TMP.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
IoUtil.closeSilently(fos);
}
} else {
showCenterToast("Uri is NULL");
}
}
private Bitmap decodeUriAsBitmap(Uri uri){
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
I have not test whether my code was correct, but I think you can fix the bugs.
I Solved It and Working Now
Basically when cropping image in fragment the issue is here, when you use activity you should pass intent in this way in on activity result for best approach to cropping follow this library
compile 'com.theartofdev.edmodo:android-image-cropper:2.5.+'.
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(getActivity());
When you use Fragment you should use:
Uri selectedImageUri = data.getData();
CropImage.activity(selectedImageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(getContext(), this);
Sometimes getcontext requires api 23 this is because you using app.fragment, so use android.support.v4.app.
Cropping Image with activity and Fragment is Different, see here i have Successfully Implement this thing.follow the link here, for guideline.
how cropping image with in activity and fragment !!!
I was feeling difficulty while cropping image in fragment so its not solved. First you take image from camera or gallery.
private void openGallery() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_CODE_GALLERY);
}
In case of taking image from camera don't forget to include file provider Manifest fil , if you fee trouble then before doing next follow this link.
Android - file provider - permission denial
private String mCurrentPhotoPath;
private void openCameranoughat() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
Uri photoURI = null;
try {
File photoFile = createImageFile();
path = photoFile.getAbsolutePath();
photoURI = FileProvider.getUriForFile(getActivity(),
BuildConfig.APPLICATION_ID + ".provider",
createImageFile());
} catch (IOException ex) {
Log.e("TakePicture", ex.getMessage());
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
takePictureIntent.setClipData(ClipData.newRawUri("", photoURI));
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivityForResult(takePictureIntent, REQUEST_CODE_CAPTURE);
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
try {
// Make sure the Pictures directory exists.
path.mkdirs();
} catch (Exception e) {
}
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + file.getAbsolutePath();
return file;
}
Now on activity result for Fragment Activity you have to write this code.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case ACTION_REQUEST_EDITIMAGE://
handleEditorImage(data);
break;
case REQUEST_CODE_GALLERY:
if (mCurrentPhotoPath == null) {
Uri selectedImageUri = data.getData();
CropImage.activity(selectedImageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(getContext(), this);
}
break;
case REQUEST_CODE_CAPTURE:
try {
Uri imageUri = Uri.parse(mCurrentPhotoPath);
if (imageUri == null) {
} else {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(getContext(), this);
MediaScannerConnection.scanFile(getActivity(),
new String[]{imageUri.getPath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
mCurrentPhotoPath = null;
}
} catch (Exception e) {
}
break;
case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE:
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Uri resultUri = result.getUri();
if (resultUri != null) {
path = resultUri.getPath();
if (TextUtils.isEmpty(path)) {
return;
}
Intent it = new Intent(getActivity(), EditImageActivity.class);
it.putExtra(EditImageActivity.FILE_PATH, path);
File outputFile = FileUtils.getEmptyFile("tietu"
+ System.currentTimeMillis() + ".jpg");
it.putExtra(EditImageActivity.EXTRA_OUTPUT,
outputFile.getAbsolutePath());
startActivityForResult(it,
ACTION_REQUEST_EDITIMAGE);
}
break;
case CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE:
Toast.makeText(getActivity(), "error in cropping", Toast.LENGTH_SHORT).show();
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
Use intent to get image:
Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickImageIntent.setType("image/*");
startActivityForResult(pickImageIntent, 100);
Use Activity Result to receiver data and get Uri.
Use android-crop to crop and get image.
On Lollipop, the cropped image is returned as a String reference to a uri in the result data action. Example:
final String action = data.getAction();
Uri imageUri = Uri.parse(action)
I have a question about how to take an image using the camera intent (or camera API) and then bring the image into an imageView for me to display in my application. This is what I have so far.
I setup a button
Button btnPicture = (Button) findViewById(R.id.btn_picture);
btnPicture.setOnClickListener(this);
I setup a Camera method
private void Camera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_CODE);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
And this is where I am lost. I am trying to process the image that I took.
private void processImage(Intent intent) {
setContentView(R.layout.imagelayout);
ImageView imageView = (ImageView)findViewById(R.id.image_view);
cameraBitmap = (Bitmap)intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
My intent is to display the image that you took inside image_view. I am not receiving an error, nothing happens. When I take the picture, I am asked to either take another picture or after I use the device back button the application force closes. It seems that I am taken out of my application completely, and returning is a big issue. Any suggestions? What am I missing?
O yea, and here is my onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(TAKE_PICTURE_CODE == requestCode) {
Bundle extras = data.getExtras();
if (extras.containsKey("data")) {
Bitmap bmp = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] image = baos.toByteArray();
if (image != null) {
Log.d(TAG, "image != null");
}
} else {
Toast.makeText(getBaseContext(), "Fail to capture image", Toast.LENGTH_LONG).show();
}
}
}
I am trying to put the image in getExtras, and then store it to a ByteArray. Was another thing I was trying to do. Not sure how it all comes together.
The method which i found to be easy and helpful is this:
MainActivity
private static String root = null;
private static String imageFolderPath = null;
private String imageName = null;
private static Uri fileUri = null;
private static final int CAMERA_IMAGE_REQUEST=1;
public void captureImage(View view) {
ImageView imageView = (ImageView) findViewById(R.id.capturedImageview);
// fetching the root directory
root = Environment.getExternalStorageDirectory().toString()
+ "/Your_Folder";
// Creating folders for Image
imageFolderPath = root + "/saved_images";
File imagesFolder = new File(imageFolderPath);
imagesFolder.mkdirs();
// Generating file name
imageName = "test.png";
// Creating image here
File image = new File(imageFolderPath, imageName);
fileUri = Uri.fromFile(image);
imageView.setTag(imageFolderPath + File.separator + imageName);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent,
CAMERA_IMAGE_REQUEST);
}
and then in your activity onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CAMERA_IMAGE_REQUEST:
Bitmap bitmap = null;
try {
GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
bitmap = getImageThumbnail.getThumbnail(fileUri, this);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Setting image image icon on the imageview
ImageView imageView = (ImageView) this
.findViewById(R.id.capturedImageview);
imageView.setImageBitmap(bitmap);
break;
default:
Toast.makeText(this, "Something went wrong...",
Toast.LENGTH_SHORT).show();
break;
}
}
}
GetImageThumbnail.java
public class GetImageThumbnail {
private static int getPowerOfTwoForSampleRatio(double ratio) {
int k = Integer.highestOneBit((int) Math.floor(ratio));
if (k == 0)
return 1;
else
return k;
}
public Bitmap getThumbnail(Uri uri, Context context)
throws FileNotFoundException, IOException {
InputStream input = context.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;// optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -1)
|| (onlyBoundsOptions.outHeight == -1))
return null;
int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
: onlyBoundsOptions.outWidth;
double ratio = (originalSize > 400) ? (originalSize / 350) : 1.0;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inDither = true;// optional
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
input = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
return bitmap;
}
}
and then on the ImageView onclick method will be like this:
public void showFullImage(View view) {
String path = (String) view.getTag();
if (path != null) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + path);
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
}
}
To take photo correctly you should store it in temp file, because data in result intent can be null:
final Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
final String pickTitle = activity.getString(R.string.choose_image);
final Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
if (AvailabilityUtils.isExternalStorageReady()) {
final Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final Uri fileUri = getCameraTempFileUri(activity, true);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent });
}
activity.startActivityForResult(chooserIntent, REQUEST_CODE);
And then get photo from Uri:
if (requestCode == ProfileDataView.REQUEST_CODE
&& resultCode == Activity.RESULT_OK) {
final Uri dataUri = data == null ? getCameraTempFileUri(context,
false) : data.getData();
final ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(imageUri, "r");
final FileDescriptor fd = pfd.getFileDescriptor();
final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd);
}