I have a code in which I call the gallery to select an image that i show in a imageview . This works fine and maintains the aspect ratio.
The problem is when the image size is larger than 2048 x 2048
I am using this code when it is the case:
uriIsNowAbitmap =
MediaStore.Images.Media.getBitmap(this.getContentResolver(),
selectedImageUri);
//...
int height = uriIsNowAbitmap.getHeight();
int width = uriIsNowAbitmap.getWidth();
if ((width>=2048)||(height>=2048)) {
int newheight = height/10; // height in pixels
int newwidth = width/10; // width in pixels
Bitmap avatarScaled = Bitmap.createScaledBitmap(uriIsNowAbitmap, newwidth, newheight, true);
previewNewAvatar.setImageBitmap(avatarScaled);
}
also works correctly, but with a problem. The images are not of the type shown rotated landscape
to try to explain, I put this example. This is the picture of the gallery:
when I select the picture and assign it to the imageview is shown rotated:
I do not understand why. I've tried a thousand ways and have read a lot of information about resize, i have read threads and sample code for this website (Android: high quality image resizing / scaling) and and many more... but nothing helps.
The intention is to show in this way ...make it with photoshop :)
I appreciate any help, took many hours trying to fix it
Regards
Firstly, you will have to detect the orientation change, and when you detect a change to landscape orientation you just rotate your bitmap accordingly. You will have to declare your Bitmap avatarScaled as a global variable in order to access it in the onConfigurationChanged() method below:
Use the onConfigurationChanged method of Activity to detect change in orientation. See the following code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(avatarScaled, 0, 0, avatarScaled.getWidth(),
avatarScaled.getHeight(), matrix, true);
previewNewAvatar.setImageBitmap(rotated);
}
}
You also have to edit the appropriate element in your manifest file to include the android:configChanges Just see the code below:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
Related
I would like to confirm that what I am doing is indeed the correct way as some elements behave unexpected.
First, I have a landscape and portrait layout, as I understand, doing this will automatically detect if the phone is in portrait/landscape mode:
- layout
- activity_video_player.xml
- layout-land
- activity_video_player.xml
Then when the user selects a video from the gallery, I check if the video was taking in landscape or portrait, by doing this (inside OnCreate):
int w;
int h;
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(this, videoURI);
String height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
w = Integer.parseInt(width);
h = Integer.parseInt(height);
if (w > h) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
I have tested this and it works fine, but I noticed some of my xml elements (play button) is placed incorrectly.
So my app flow is:
MainActivity --> SelectvidButton --> Gallery Intent --> VideoPlayActivity
My Question
Is this the correct way of doing this and if it is, is there any reason why some of the xml elements get placed incorrectly?
EDIT 1:
I noticed that this only happens when the activity is launched for the first time, if I press the back button and select the same video again, the layout is perfectly like I want it to be.
EDIT 2:
I have also noticed that this only happens if the previous activity (MainActivity) was in the same orientation than what the selected video is.
Here is what I ended up doing.
Instead of using MediaMetadataRetriever to get the width and height, I first retrieve a Bitmap from the video file and then setting the orientation according to the width and hight of the Bitmap, as shown below:
private void rotateScreen() {
try {
//Create a new instance of MediaMetadataRetriever
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//Declare the Bitmap
Bitmap bmp;
//Set the video Uri as data source for MediaMetadataRetriever
retriever.setDataSource(this, mVideoUri);
//Get one "frame"/bitmap - * NOTE - no time was set, so the first available frame will be used
bmp = retriever.getFrameAtTime();
//Get the bitmap width and height
videoWidth = bmp.getWidth();
videoHeight = bmp.getHeight();
//If the width is bigger then the height then it means that the video was taken in landscape mode and we should set the orientation to landscape
if (videoWidth > videoHeight) {
//Set orientation to landscape
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
//If the width is smaller then the height then it means that the video was taken in portrait mode and we should set the orientation to portrait
if (videoWidth < videoHeight) {
//Set orientation to portrait
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
} catch (RuntimeException ex) {
//error occurred
Log.e("MediaMetadataRetriever", "- Failed to rotate the video");
}
}
There are several stack overflow questions that deal with keeping the activity in a constant orientation, suggesting either to set android:screenOrientation (or this) or to deal with the configuration changes manually.
However, these answers do not solve the problem if one wants to keep the benefits of automatic layout for most of the views, but to keep one view in a constant orientation. For example I have one main view for the content (a bitmap) and optionally several "toolbars". When the device is rotated from portrait to landscape, I want the toolbars to re-layout automatically. On the other hand, the main content view has a bitmap, and this should rotate with the device. Here is a simple code I use in my onDraw:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bm = ... // code to get a bitmap
if (bm != null) {
canvas.drawBitmap(bm,0,0,mPaint);
}
}
This obviously does not work as intended because when the device orientation changes, the view is relayout and its coordinate system rotates in this orientation change by 90 degrees with respect to the physical device. One way to compensate for that would be to use a Matrix-object to draw the bitmap with 90 degrees rotation if the device orientation has changed. However, then also the input coordinates would have to be changed in order to keep the view input points and the point on the bitmap in correspondence.
Therefore my question after this long explanation is: can I keep the view so that its coordinate system with respect to the device is not rotated when the orientation changes.
Here is a picture that expresses what I want and what I currently get (xy coordinate system for the view also illustrated).
Inside your activity in manifest xml use the screen orientation attribut with portrait value
Like
<activity
android:name=".YourActivityName"
android:screenOrientation="portrait" >
</activity>
I am not sure but you should try following
put one xml in layout folder which indicate portrait layout
create one folder named it layout-land
put xml which indicate landscape layout with same name as in layout folder
Android automatically take that layout from land-layout when you will rotate your screen to landscape.
Another way
Activity will be restarted when orientation is changed so make to layout for that activity and you can set layout to your activity as per orientation.
if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.landscape_layout);
}
else
{
setContentView(R.layout.portrait_layout);
}
Hope it will help you.
You can check the orientation and redraw your View after orientation will be changed
if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
{
//...
}
else
{
//...
}
Use this code in your Manifest
<uses-permission android:name="android.permission.SET_ORIENTATION"/>
<activity
android:name="com.package.MyActivity"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait" >
</activity>
Well, first of all, you will not need two layouts.
Create only one xml with an image view and a layout which has property align parent bottom set to true.
Now as per the images you have provided, you want to rotate the image anticlockwise when your device is in landscape. For that , you can use my image rotation code :-
You can always check the rotation of the image using Matrix and rotate it accordingly.
This code goes in onCreate-->
The code below is to rotate the image when taken from camera or gallery. You can modify according to your needs. I have put comments below to simplify understanding.
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = false;
bmOptions.inPurgeable = true;
Bitmap cameraBitmap = BitmapFactory.decodeFile(filePath);//You convert your image view image here in a bitmap
ByteArrayOutputStream bos = new ByteArrayOutputStream();
cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
ExifInterface exif = new ExifInterface(filePath);
float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
System.out.println(rotation);
float rotationInDegrees = exifToDegrees(rotation);
System.out.println(rotationInDegrees);
//These three lines below set the rotation.
//Put an if condition here to check device rotation and set the rotation of image view as per the device rotation.
//The line below will help you get device rotation.
//getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE
Matrix matrix = new Matrix();
matrix.postRotate(rotationInDegrees);//Here you set the rotation value in which you want your image view to be rotated
//If ends here
Bitmap scaledBitmap = Bitmap.createBitmap(cameraBitmap);
Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
FileOutputStream fos=new FileOutputStream(filePath);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
//this rotatedbitmap you can set in your image view
//onCreate Code Ends here.
//This function below is used to get rotation:-
private static float exifToDegrees(float exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}
I make use of the following code to set my static images as wallpaper from my android app.. The image dimensions are like 425*700, 280*180, 600*400 etc., so the image dimensions are not the same.
try {
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(context);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int fullWidth = size.x;
int fullHeight = size.y;
// int fullWidth = wManager.getDesiredMinimumWidth();
// int fullHeight = wManager.getDesiredMinimumHeight();
Log.d("Debug", Integer.toString(fullWidth));
Log.d("Debug", Integer.toString(fullHeight));
Bitmap bitmap = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.hello));
Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap, fullWidth,
fullHeight, true);
myWallpaperManager.suggestDesiredDimensions(
bitmapResized.getWidth(), bitmapResized.getHeight());
myWallpaperManager.setBitmap(bitmapResized);
} catch (IOException e) {
e.printStackTrace();
}
But the images are quite streched and doesn't look good after setting as a wallpaper in the phone.. What am I doing wrong?
I guess problem lies in the resources as you have already mentioned. For a good view/quality of wallpaper we have to use different resources of images as according to the resolution of your device, if you want to use your application of setting the wallpaper to be used by many devices without making any changes(an apk). The crux is, you need a better quality image matching the resolution of your device. Hope it helps.
Edit: The distortion is again because you are resizing your image for covering full screen size. I guess if you do not resize it, it might work, though I havent use it till now.
I'm writing an app that uses the phone's camera to take a picture, and then display it. The problem is, for phones that have high-res cameras, this caused the app to crash, so I lowered the resolution using isSampleSize, which solved this issue. But now I have a problem with phones that have lower resolution cameras - the picture has terrible quality. Is there any way to check what the image memory consumption is, and according to that decide whether I want to lower the quality or not?
To rescale my picture, because I had a limitaion in the image height, I used the code below to resize it. But i didnt wan't to resize the bmp unless it was above my height requirement, hope you can use it. (Used a stream to get my pictures in)
if (stream != null) {
img = BitmapFactory.decodeStream(stream, null, o);
float imgHeight = (float) img.getHeight();
Display display = ((WindowManager) ctx.getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay();
float displayHeight = (float) (display.getHeight()-50);
if(imgHeight>displayHeight) {
float scaleFactor = displayHeight/imgHeight;
img = Bitmap.createScaledBitmap(img, (int)(img.getWidth()*scaleFactor), (int)(imgHeight*scaleFactor), false);
}
saveBitmap(new File(getSdPath() + location), img);
}
What I'm doing seems like it should be simple, but I'm still lost after I've read every possible Stackoverflow answer I can find and Googled every article I can find.
I'm using a preview SurfaceView and capturing an image from an activity that is set for screenOrientation="landscape" in my AndroidManifest.xml.
I followed the sample Camera app code and thought things were working until I tried my app on a few Motorola devices running 1.5.
I have the OrientationEventListener running OK and I use reflection to see if set the rotation as such:
final int latchedOrientation = roundOrientation(mLastOrientation + 90);
Parameters parameters = preview.camera.getParameters();
JPLog.d("Setting camera rotation = %d", latchedOrientation);
try {
// if >= 2.0
Method method = Camera.Parameters.class.getMethod("setRotation",
int.class);
if(method != null) {
method.invoke(parameters, latchedOrientation);
}
} catch(Throwable t) {
// if < 2.0
parameters.set("rotation", latchedOrientation);
}
preview.camera.setParameters(parameters);
NexusOne (OS 2.2) - Works great. latchedOrientation = 0, picture OK without any rotation in the EXIF header.
T-Mobile G1 (OS 1.6) - Also works great. latchedOrientation = 0, picture OK.
Motorola Backflip (OS 1.5) - Image rotated. latchedOrientation = 0, picture has no EXIF rotation in it.
Motorola CLIQ (OS 1.5) - Image rotated. latchedOrientation = 0, picture has no EXIF rotation in it.
What's going on with these Motorola devices? I thought my problem was the Motorola camera driver wasn't rotating the images, so found the Sanselan EXIF reading classes for Android and was preparing to rotate them myself. Funny thing is, there is EXIF headers but no rotation element.
If I set the rotation manually to 90 degrees, the images come out perfect the Motorola devices, but now the G1 and the NexusOne have images that are rotated 90 degrees (not what I want). There has to be something I'm not getting here.
I'm doubting this is a 1.5 issue, or else someone would've posted info on it?
I had this issue and I used this method to capture the image. (Without creating a custom camera)
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
startActivityForResult(intent, 0);
and did the rest in onActivityResult(int requestCode, int resultCode, Intent data) {}
But the original image (Actual SD card image) was correct and Bitmap was rotated when I fetched like this.
Bitmap bmp = BitmapFactory.decodeStream(..
The solution:
try {
File f = new File(SD_CARD_IMAGE_PATH);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
Bitmap correctBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
}
catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
}
catch(OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}
This is actually a device-specific issue that mostly affects Motorola devices. The google devs included a setDisplayOrientation call in API 8 to work around the issue. The main bug is filed here.
For those that can't go to API 8, the two common solutions are:
Override onDraw
Override onDraw in a top-level ViewGroup and rotate the canvas by 90 degrees to compensate for the rotation. Note there is a caveat here as your touch events will also need to be rotated.
Use Landscape Mode
Lock the activity to landscape mode but draw assets as if they are in portrait. This means you do your layout and rotate your image assets to look like you are in portrait mode so that the view looks normal. This unfortunately makes it difficult to use the menu since the menu will open horizontally.
I have also seen people use an animation controller to rotate the view. The drawback here that I wasn't able to overcome is that the rotated view doesn't stretch to fill the screen. See the answer by Georg for a sample implementation.
Here is the code I used onActivityResult() in my activity. The intent returned was for picking an image of the type image/*. Works well for me!
Uri imageUri = intent.getData();
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
It looks like the 'use landscape mode' suggestion is the only thing that really works. It seems that it's ok for this to be in either the manifest, or done via a call to setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) in the activity onCreate.