How to display Photograph without distortion in Android Application - android

My current Android Application allows the user to select any photograph they have on their device.
I use this code to display all photographs to allow user selection
final Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
Once the photograph has been selected, i display it within my applications main activity.
My main activity employs a LinearLayout as follows:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10" >
<ImageView
android:id="#+id/photograph"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/select_photo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:text="Photo Select" />
</LinearLayout>
I use this code to display the photograph in my application
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri imageUri = data.getData();
try {
final Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The selected photographs are never dislayed correctly, e.g. they are distorted to a lesser or greater degree.
How do i guarantee that any photograph selected is displayed with the correct dimensions/proportions?

Either size your ImageView to "the correct dimensions/proportions", or use android:scaleType to indicate how you want the image scaled when it is applied to the ImageView.

Related

Android: Taking picture with low quality [duplicate]

This question already has answers here:
Low picture/image quality when capture from camera
(3 answers)
Closed 3 years ago.
I created a project that allow user to take a picture and view it. I have no face any issue while taking, view, save and retrieve the photo. But my problem is the image that camera took is in bad quality, I don't know how to set the quality of the camera Intent. Here is my code looks like.
....
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
....
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap userPhoto = (Bitmap) data.getExtras().get("data");
iv_user.setImageBitmap(userPhoto);
}
}
Here is a screenshot of while camera is on.
And here is in preview camera mode after the image is captured.
If we compare these two photos, we can see the image in preview mode is blur.
Im actually not sure why it's like that but it's same here after every method tried, so I suggest the solution that you make use of a very simple library like below, good things is your picture quality remains intact.
simple add to your gradle file:
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
Simple call startCameraAndOptions(); method to start you camera
startCameraAndOptions(); //call to starts you camera with other additional option like gallery.
public void startCameraAndOptions(View view) {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setActivityTitle("My Crop")
.setCropShape(CropImageView.CropShape.RECTANGLE)
.setCropMenuCropButtonTitle("Done")
// .setRequestedSize(400, 400)
.setAllowRotation(true)
.setOutputCompressQuality(100)
.setCropMenuCropButtonIcon(R.drawable.arrow)
.start(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
BitmapDrawable background;
Bitmap bitmap;
ImageView img_preview = (ImageView) findViewById(R.id.img_preview);
// handle result of CropImageActivity
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), result.getUri());
background = new BitmapDrawable(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Picasso.get()
.load(result.getOriginalUri())
// .resize(200, 200)
// .centerCrop()
.into(img_preview);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
}
}
}
**/* LAYOUT SECTION */ optional (style it your own way)**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hollaport.hollaport.Demo">
<Button
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:id="#+id/trigger"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/camera"/>
<!--<com.theartofdev.edmodo.cropper.CropImageView-->
<!--android:id="#+id/cropImageView"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="0dp"-->
<!--android:layout_weight="1"/>-->
<android.support.v7.widget.CardView
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:innerRadius="0dp"
android:shape="rectangle"
android:thicknessRatio="2"
app:cardCornerRadius="0dp">
<ImageView
android:id="#+id/img_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</android.support.v7.widget.CardView>
</LinearLayout>
The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap (we call it thumbnail) in the extras, under the key "data".
If you want to retrive full size image your app first need to store image to external storage and then read it back. You can access full code at
https://developer.android.com/training/camera/photobasics

Choose image from gallery, display in imageview and save image displayed in a new directory

I have two buttons and an ImageView in my Activity. When I click on one button it would redirect to gallery and I would be able to select an image. The selected image would appear in my ImageView.
The problem now is i want the save button to save the image displayed in a new directory like it does in apps like instagram.
My layout is given below;
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="imgClick"
android:id="#+id/imgButton"
android:text="Select Image"
android:layout_marginTop="20dp" />
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_centerHorizontal="true"
android:id="#+id/diddyImageView"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:maxWidth="100dp"
android:maxHeight="100dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="addMovieClick"
android:id="#+id/addMovieButton"
android:text="Submit Movie"
android:background="#color/colorPrimary"
android:layout_marginTop="20dp" />
My activity is given below
public void imgClick(View v){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECTED_PICTURE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECTED_PICTURE && resultCode == RESULT_OK) {
uri = data.getData();
diddyImageView.setImageURI(uri);
}
}
The image is displayed properly, i want to save the image in a new folder and i dont know how to go about it..Any help will be appreciated..Thanks
Step #1: Use a ContentResolver and openInputStream() to open an InputStream on the Uri to the image
Step #2: Open a FileOutputStream on the desired file for your copy
Step #3: Use standard Java I/O to copy the bytes from the InputStream to the FileOutputStream
Step #4: Move all of that work into a background thread (and consider using an image-loading library to replace your setImageURI() call), to avoid tying up the main application thread doing all of this work

I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?

I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?
This works fine.. for selecting and then displaying the image..now what do i do for selecting next and previous images?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
#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) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I could select an image from gallery in android
Your code happens to bring up a gallery app on your device. You are requesting ACTION_GET_CONTENT for image/*. This means:
Other apps, besides a gallery, can respond to that Intent and can be chosen by the user
There will be devices that do not have an app that would be considered a "gallery"
How do i do it?
Write your own gallery. There is no concept of "next" or "previous" with ACTION_GET_CONTENT.
There are many image picker libraries for Android that might form the basis for your own gallery UI.
I forgot to write the most important thing, which is code for previous and next is not working.. till now i have written code only for previous, coz once it works; next ll be just few changes ahead.
moveToprevious is coming false n cursor is declared as global.. so that when a query for cusor s fired in getcontentresolver() is fired, it gets the needed value..
Please look into this!

Android app photo uploading problems

I am a newbie on android app development. My team and I are creating an app that uses photo gallery folders to upload the photo. The app is able to take a photo and upload that photo immediately. But specifically with samsung devices, when I try to upload photo from camera folder, app either crashes or uploads a white page. When I try a different folder from the gallery besides than camera folder, that works perfect too. It gives this error only with camera folder. I tested my app with different nexus devices and it worked absolutely perfect. I have been searching this any where and everywhere. I wrote all the codes on Android Studio along with Java and XML. Did anyone have the same issue and were able to solve? I really need a guidance. Thanks for your time and your attention.
Here is my Java onGalleryClick method:
public void onGalleryClick(View v) {
isCameraReleased = true;
//TODO
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
I think I figured it out with implementing Picasso library. Finally I can access to Camera folder( Some of the devices it is DCIM) when I got into gallery option. I believe Samsung devices has a very low virtual memories and somehow it doesn't allow you to see the imageView on the android app that you create. Basically I created a very simple button on android studio and it's called activity_main.xml. Which is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ImageView>
<Button
android:id="#+id/buttonLoadPicture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0"
android:onClick="loadImagefromGallery"
android:text="Load Picture" >
</Button>
</LinearLayout>
Afterward, I created MainActivity.java that gives all the functionality. Which is:
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
And this is the line of code that you have to add on AndroidManifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Here is the tricky part of everything. in your build.gradle(Module:app) you are going to add
compile 'com.squareup.picasso:picasso:2.3.3' at dependencies section.
After you gotta add
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg")
.into(imageView);
And there you go!Please take a look http://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149

Start camera for capturing picture and store image after captureing it in gellary on button click

I am working on android application. I have an Activity in which there is two button first one for selecting image from gallary. i have applied function on it. i have one more button capture image . i want to work on it .but don't know how to start camera .I want that when i click button capture image it should start camera for capture image.and there should be option to cancel if don't want to take picture. after pressing cancel camera should cancel.
if i captures image it should it should show in Image View and automatically store in SD card .how should i proceed.
http://developer.android.com/guide/topics/media/camera.html. Everything you need to know about starting a camera. Go through the link.
private static final int TAKE_PHOTO_CODE = 1;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );
startActivityForResult(intent, TAKE_PHOTO_CODE);
get uri
private File getTempFile(Context context){
return new File(path, "/tourpath/yourfilename.jpg");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode){
case TAKE_PHOTO_CODE:
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file));
iv.setImageBitmap(captureBmp);//show in imageview
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}

Categories

Resources