Picture taken from camera getting blur - java

I have integrated crop image library in my application which have function for take and use picture taken via camera. My developer have done it as expected but now when I have checked via take picture from camera than after take picture and set it on crop page, its getting blur before we set it.My developer is out of coverage for some task. I have asked library developer and they have given me solution for integrate code like below
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Uri outputFileUri = Uri.fromFile(new File(context.getExternalCacheDir().getPath(), "pickImageResult.jpeg"));intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
and My developer have integrated code like below in my application
#Override
public void onClick(DialogInterface dialog, int which) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent,REQ_PHOTO_CAMERA);
}
}
as well method like below
public final int REQ_PHOTO_CAMERA=243;
public final int REQ_PHOTO_GALLERY=346;
public final int REQ_APP_GALLERY=427;
public final int ACTION_CHANGE_BACKGROUND=1;
public final int ACTION_CHANGE_AUTHOR=2;
private int mChangeAction;
public void onActivityResult(int req,int res,Intent data){
if(res==RESULT_OK){
if(req==REQ_PHOTO_CAMERA){
Bitmap cameraImg = (Bitmap) data.getExtras().get("data");
cropAndSaveImage(cameraImg);
//updateCustomImage(cameraImg);
}else if(req==REQ_PHOTO_GALLERY){
try {
Bitmap imgGallery = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
// updateCustomImage(imgGallery);
cropAndSaveImage(imgGallery);
} catch (IOException e) {
// e.printStackTrace();
}
}else if (req == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Uri resultUri = result.getUri();
Log.e("ImageUrl",resultUri.getPath());
updateCustomImage(BitmapFactory.decodeFile(resultUri.getPath()));
}else if(req==REQ_APP_GALLERY){
String imgPath=data.getStringExtra("ImagePath");
try {
InputStream inputStream=getAssets().open(imgPath);
Bitmap image=BitmapFactory.decodeStream(inputStream);
cropAndSaveImage(image);
} catch (IOException e) {
}
}
}
}
public void cropAndSaveImage(Bitmap imgPicked){
ImageLoader.getInstance().saveTempImage(imgPicked);
CropImage.activity(ImageLoader.getInstance().getTempImageUri())
.setInitialCropWindowPaddingRatio(0)
.setFixAspectRatio(false)
.setAspectRatio(1,2)
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
let me know what I am missing ?
Note : we have used this library : Link
Thanks

Your original image taken using camera will be here-
Uri outputFileUri = Uri.fromFile(new File(context.getExternalCacheDir().getPath(), "pickImageResult.jpeg"));
in this file.
the Bitmap cameraImg = (Bitmap) data.getExtras().get("data"); is just a thumbnail returned.

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

Wait the result of an activity

I have a class(A) who need to take a picture, so I create an other class(B) with this responsibility. But the problem is, that A don't wait the result of B. How can I do that?
Here is my code :
Class which take the picture
static final int REQUEST_IMAGE_CAPTURE = 1;
Bitmap image = null;
public void takePicture(Context context, Activity activity) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
activity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
image = (Bitmap) extras.get("data");
}
}
public Bitmap getImage(){
return image;
}
The method which call the the Camera class
public void openCamera() {
Bitmap image = null;
camera.takePicture((Context) _addView, (Activity) _addView);
image = camera.getImage();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
byte[] picture = outStream.toByteArray();
_addView.SetCameraPicture(picture);
}
While taking pictures what you are doing is opening another app(startActivityForResult) which is capable of taking pictures. When the app finishes taking the picture you are sent the data through an intent which you listen using onActivityResult. The other class B must be an
Activity(startActivityForResult() can be called only from an Activity).
So with your scenario, you must first open Activity B, then take the picture,
put the result into an intent and start Activity A with that data, which just does not make any sense. If you want to follow DRY principles put your photo taking method into a utility class and dependency inject your class to wherever you need it. Hope this helps.
You can always try to make class B async and then let class A await the method of class B

How can I save an image to my app?

I am making an app that will let users take photos and save them to the app, which will be password protected. So far, the app can take a picture, retrieve it, and set it to an image view. However, when I restart the app the image goes away. How can I save it?
int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
Uri imageUri;
public void takePic(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "filename_" +
String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra("data", imageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
Bundle extras = data.getExtras();
Log.e("URI", imageUri.toString());
Bitmap bmp = (Bitmap) extras.get("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bmp);
}
else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
}
That imageUri you pass to the Intent- the image file is saved there. Just save the URI in SharedPreferences or other persistant storage and check that storage next time you launch your app.
This code is working on me :
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
mImageCaptureUri = null;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mImageCaptureUri = Uri.fromFile(mFileTemp);
}
else {
mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
}
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
} catch (Exception e) {
Log.d("error", "cannot take picture", e);
}
}
This is how to define mFileTemp
String state = Environment.getExternalStorageState();
File mFileTemp;
if (Environment.MEDIA_MOUNTED.equals(state)) {
//this is like that
//any folder name/you can add inner folders like that/your photo name122412414124.jpg
mFileTemp = new File(Environment.getExternalStorageDirectory()+File.separator+"any folder name"+File.separator+"you can add inner folders like that"
, "your photo name"+System.currentTimeMillis()+".jpg");
mFileTemp.getParentFile().mkdirs();
}
else {
mFileTemp = new File(getFilesDir()+"any folder name"+
File.separator+"myphotos")+File.separator+"profilephotos", "your photo name"+System.currentTimeMillis()+".jpg");
mFileTemp.getParentFile().mkdirs();
}
Your global variables
private Uri mImageCaptureUri;
private File mFileTemp;
1) Define your global variables
2) Then define mFileTemp
3)Then trigger takePicture() method

how can i send image via activities in android

I have an application that allow users to choose picture from native gallery then I show this image in image view widget.
My question is:
1-i have to send this image to another Activity. How can i do it.
2-in the receiver Activity i should show it in image view widget as in image not link or Something
I tried this code but it gives me a RunTime Error
Bitmap image = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.RGB_565);
 view.draw(new Canvas(image));
 String url = Images.Media.insertImage(getContentResolver(), image,"title", null);
Just follow the below steps...
Uri uri = null;
1) on any click event use the below code to open native gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),0);
This will open gallery select picture will return you to your activity. OnActivity result.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
try {
uri = Uri.parse(data.getDataString());
imageView.setImageUri(uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
break;
}
}
2) Also instead of passing the image you can pass the URI to next activity as you pass string and inthe secont activity you get it using intent.
Intent i = new Intent(this, Second.class);
i.putExtra("URI", uri.toString() );
startActivity(i);
and in the second activity
String uri = getIntent().getStringExtra("URI");
Now you have string just set it to the image view like below
imageView.setImageUri(Uri.parse(uri));
Use intent putExtra and send uri of the image user selected in Acvtivity1 and in second activity use intent getExtra to read the uri
Refer this answer https://stackoverflow.com/a/7325248/308251
Maybe this is not what you're looking for and it's a bit poor but saved my life when I needed to pass objects between Activities.
public class MagatzemImg {
private static MagatzemImg instance = null;
private static Bitmap img;
public MagatzemImg(){
img=null;
}
public static MagatzemImg getInstance() {
if (instance == null)
instance = new MagatzemImg();
return instance;
}
public static void setImg(Bitmap im){
img = im;
}
public static Bitmap getImg(){
Bitmap imgAux = img;
img = null;
return imgAux;
}
}
And then from the new activity:
MagatzemImg.getInstance();
image = MagatzemImg.getImg();
You can 'assure' to the new Activity that the image exists inside the Static Class through putExtra("image",true) or something else you prefer, like checking if the "image" is null.

Categories