Folks.
I'm facing a strange issue here. I have an application that you can select an image from Gallery or Camera in a button click. All went well except whenever i select any picture from my camera folder the application just crashed. Hereunder my code
case R.id.camera:
AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Gallery", "Camera" },
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent chooser = Intent.createChooser(intent,
"Choose a Picture");
startActivityForResult(chooser,
ACTION_REQUEST_GALLERY);
break;
case 1:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String mImageCaptureUri = null;
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
startActivityForResult(cameraIntent,
ACTION_REQUEST_CAMERA);
break;
}
}
});
builder.show();
break;
Activity for result :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ACTION_REQUEST_GALLERY:
Uri selectedImageUri;
selectedImageUri = data == null ? null : data.getData();
cap.setImageURI(selectedImageUri);
break;
case ACTION_REQUEST_CAMERA:
Bitmap photo = (Bitmap) data.getExtras().get("data");
cap.setImageBitmap(photo);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
Toast.makeText(getApplicationContext(),
finalFile.getAbsolutePath(), 4000).show();
break;
}
}
}
It is hard to say without logcat, but most probable reason is large image size, photos taken with camera are to big to show without scaling, this is the reason that some images you tried works while camera images don't. Try to google: scaling larg bitmaps google developers and you will find official post
Related
I have some troubles to display images in OpenGL.
Actually I'm able to display images from gallery in opengl. The problem occurs when I try to show one from the camera.
For me, OpenGL have to display the image from the camera as it does with the gallery ones. Obviously I'm making something wrong.
Any help will be appreciated.
Intent from gallery:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/");
startActivityForResult(intent, 2);
Intent from camera:
Intent takePic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePic.resolveActivity(getPackageManager()) != null) {
File imagen = controler.createPhotoFile(getExternalFilesDir(Environment.DIRECTORY_PICTURES));
if (imagen != null) {
photoUri = FileProvider.getUriForFile(this, "my.fileprovider", imagen);
takePic.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePic, 1);
}
}
This is my onActivityResult where I send the URI to a method which convert it to a bitmap and send it.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
sendImagenPanel(photoUri);
break;
case 2:
sendImagenPanel(data.getData());
break;
}
}
}
private void sendImagenPanel(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
final Bitmap imagen = controler.getCroppedBitmap(controler.scaledBitmap(bitmap, 256));
final CasillaOG casilla = ((GLSurfacePanel) gLViewPanel).getRendererPanel().getCuboSelected();
gLViewPanel.queueEvent(new Runnable() {
#Override
public void run() {
casilla.loadNewTexture(imagen);
casilla.setImagen(imagen);
}
});
gLViewPanel.requestRender();
}
In case someone is interested. I realize that the problem is not on the method that calls OpenGL. If I run the same code on the onActivityResult works from the gallery requestCode but not on the camera one, in my Samsung Galaxy Tab A. Why I mention my device? because if I run the app on a Huawei P9 lite, the gallery images are not display either. In both cases appears the next problem on the console:
call to opengl es api with no current context (logged once per thread)
After search that problem, I suppose that the intents of the camera and gallery use OpenGL and its originate a conflict with my own OpenGL environment.
Finally, I opted to set a bitmap field and add the texture in on the onDrawFrame. Obviously, with a boolean to make it one time.
So I used this to open my image choose upon button click
//Open image chooser
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
and used onActivityResult
private final static int SELECT_PHOTO = 12345;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("CALLED", "OnActivity Result");
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
Log.e("img", "It worked");
// Do something with the bitmap
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
}
In logcat, OnactivityResult is not being called, and I cant figure out why. So when I click the button, the image chooser pops up, I choose an image, and then it exits back to the main screen.
Am I missing something, as I've followed others' code but I still get the same thing
Give this a try:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(photoPickerIntent,"Select:"), SELECT_PHOTO);
I want the user to be able to access their gallery (by clicking the add photo ImageView), upload a photo of their choosing, and display that photo in the circular profile photo spot. I can't seem to find any definitive guides on how to do this. What is the easiest/best way to go about it? (in Java).
(I would have commented, but I canĀ“t, since I do not have 50 rep.)
You might wanna check:
Get Image from the Gallery and Show in ImageView
(Keep in mind that this is only about loading the Image, saving would need some extra)
Cheers!
you can use this to pick your photo:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
and below code is your on activity result:
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image_view.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
I'm currently working on the user profile for my app. Up until recently, I've been testing on a Samsung Galaxy Note 4, which hasn't been giving me any problems with the code below. In later testing, however, I got my hands on a Nexus 5x, and when I use the below code to try and select a user profile image, I get the error "Unfortunately Photos has stopped". I don't have any error logs, since I am unable to debug Photos.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
cropImage(selectedImage);
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
cropImage(selectedImage);
}
break;
case 2:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
profileImage.setImageURI(selectedImage);
try {
userProfileImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
AlmightyFunctions.ImageService.saveProfileImage(user,userProfileImage);
}
catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
public void showImageSelectDialog() {
CharSequence options[] = new CharSequence[] {
getString(R.string.select_camera),
getString(R.string.select_gallery)
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.select_image_dialog));
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
MarshMallowPermission mmp = new MarshMallowPermission(UserProfileActivity.this);
switch(which) {
case 0:
if (!mmp.checkPermissionForCamera()) {
mmp.requestPermissionForCamera();
}
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);
break;
case 1:
if (!mmp.checkPermissionForExternalStorage()) {
mmp.requestPermissionForExternalStorage();
}
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);
break;
}
}
});
builder.show();
}
public void cropImage(Uri photoUri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(photoUri, "image/*"); // this will open all images in the Gallery
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1); // this defines the aspect ration
intent.putExtra("aspectY", 1);
intent.putExtra("return-data", true); // true to return a Bitmap, false to directly save the cropped iamge
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); //save output image in uri
startActivityForResult(intent,2);
}
Any help would be greatly appreciated.
Android does not have a CROP Intent. Your code will fail on hundreds of millions of devices.
There are many image cropping libraries available for Android. Please use one.
I'm making an Android application in Android studio where the user can take a photo and save it into a new folder in their gallery. Currently the application takes the pictures fine but doesn't save the image into the new "SOC" folder in my gallery. I'm not getting any errors and I have no idea why it's not saving the image. Any help would be appreciated.
My code is as fallows
static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
public void onClickbtnCamera(View v)
{
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
Uri uriSavedImage=Uri.fromFile(new File("/storage/emulated/0/DCIM/SOC","QR_"+timeStamp+ ".png"));
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, 1);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
//Check for succesful result code
if (resultCode == -1) {
//Show your Toast when the result is a success.
Toast toast = Toast.makeText(getApplicationContext(),
"Picture is saved in your SOC gallery", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 100, 0);
toast.show();
}
}
}
I think you must manually add your new photo Uri to Media Content Provider.
take a look here
Call this method in your Activity for result
protected void addPhotoToGallery() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(getCurrentPhotoPath());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.getActivity().sendBroadcast(mediaScanIntent);
}
you should save the photo path before launching the intent and then getCurrentPhotoPath() must get that path