Image is not updating - java

Everything works fine except image updation! When I register a new user and when I click in the image, it opens my image gallery, I choose any image, and it back to register activity but it won't update. Also I'm unable to register because that image is not uploading successfully and we sat if imagepath == null, show that's why toast error is coming that fill all the details.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && requestCode == RESULT_OK && data.getData() !=null){
imagePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imagePath);
userProfilePic.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
NOTE:
I tried multiple images, same issue.
There is no error in logcat.
Default image is showing perfectly when we first time try to register that time we sat android logo which is a default.
I can't register without selecting any image because image path == null.
No problem of image size.
Guys, any solution?

Related

Show an image in opengl is working with pictures from gallery but from camera doesn't

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.

How to upload profile photo to app in Android Studio

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();
}
}

Currently running fragment closes and gets back into previous activity when try to import photo from gallery

Currently running fragment closes and gets back into previous activity when trying to import photo from gallery and set it to ImageView.
I want to set the imported image to an ImageView which is in the fragment. But when I select the image it closes the current fragment and goes back to the previous activity.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
if(requestCode == 1000){
try {
Uri returnUri = data.getData();
Bitmap bitmapImage = null;
bitmapImage = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), returnUri);
ImageView iv = getView().findViewById(R.id.profile_image);
iv.setImageBitmap(bitmapImage);
getFragmentManager().popBackStackImmediate();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I want choose an image from the gallery and set it to an ImageView which is in the currently running fragment without getting closing it.
I tried putting getFragmentManager().popBackStackImmediate(); in OnActivityResult() in my fragment. But it doesn't work.

Android get URI of cropped image from Intent

I'm trying to capture an image from the Android Camera/ Pick an image from the gallery and then crop it before performing other operations on it. I'm having trouble with getting back the URI of the cropped image. Any help on how to get back the URI of the image once it has been cropped would be much appreciated!
The code below pertains to my onActivityResult and my function performing the crop
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE || requestCode == REQUEST_IMAGE_CAPTURE) {
if(!IS_CAMERA_USED) {
selectedImageUri = data.getData();
}
performCrop();
}
else if(requestCode == CROP_IMAGE){
selectedImageUri = data.getData();
onSelectedImageResult();
}
}
}
public void performCrop() {
// take care of exceptions
Display display = getWindowManager().getDefaultDisplay();
try {
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(selectedImageUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectY", 1);
if(IS_PROFILE_PICTURE) {
cropIntent.putExtra("aspectX", 1);
}
else {
cropIntent.putExtra("aspectX", 2);
}
// indicate output X and Y
cropIntent.putExtra("outputX", display.getWidth());
cropIntent.putExtra("outputY", display.getHeight());
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
Toast toast = Toast
.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}
The problem is in the line selectedImageUri = data.getData(), where data.getData() returns null after having done the crop. How do I get back the URI of the cropped image? I don't want to get data.getExtras.getParcelable("data") as that returns the thumbnail and ruins the image resolution.
Thanks in advance!
You can get the image bitmap with this code:
if (requestCode == CROP_IMAGE) {
if (data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap selectedBitmap = extras.getParcelable("data");
//imgView.setImageBitmap(selectedBitmap);
}
}
}
URI is not possible I think, because the cropped image is not saved on the storage.
Thanks for all your answers, but I found something way simpler to incorporate and use that avoids all hassles due to the `com.android.camera.action.CROP' class, and it can be found here
I think that you should save the cropped Bitmap to your storage, and then use the URI of the cropped Bitmap to performe something over it!
I know it's not a professional work but at least it should do it

Android: Compressed bitmaps from int.storage not loading dynamically

I have a small issue with bitmaps which I want to load into a view with a picture in every entry. The application looks like this:
Application layout
I actually have two problems - the first is that when i compress the file in code by 90%, the result file gives me even higher file size, f.e. file before compressing was 500kb, after its 6MB.
The second problem is that the images are saved to internal storage of the application (/com.example.my.application/files/) but are not shown in the list itself. What you see on the picture is working when i use the pictures directly from gallery using URI of the files, although because of their size, the RAM usage gets too high and after adding 5-6 of them the application crashes.
Here is the code for getting selected picture, compressing and saving:
#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) {
File mypath=new File(new File(this.getApplicationContext().getFilesDir(),"")+"/"+data.getData().getLastPathSegment()+".jpg");
try {
FileOutputStream fos = new FileOutputStream(mypath);
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
imageURI = data.getData().getLastPathSegment();
TextView tv = (TextView) findViewById(R.id.note_image_uri) ;
tv.setText(imageURI);
}
}
This is the adapter of a gridview:
#Override
public void onBindViewHolder(SolventViewHolders holder, int position) {
...
Uri uri = Uri.parse(context.getFilesDir()+"/files/"+itemList.get(position).getUrl());
//Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(image));
holder.countryPhoto.setImageURI(uri);
}
So basically the idea is that after an image from gallery is selected, i want to compress it, save it into /data/com.example....../ under the name specified by .getLastPathSegment(), and then load the picture using the same path with the last path segment which is unique for every object in the view. I guess Im missing something small, but Im not that good at it, so I need your help. Thanks for any help in advance!

Categories