I'm little confused about setting up wallpaper,
In my galaxy s if i set wallapaper through code then it's not scrolling by default but If galary sets the wallpaper then its scrolling
Avoid a wallpaper to stretch across 5 screens
It depends on Home luncher app but how can i manage scrolling enabled disabled in my code
// the content of our screen.
setContentView(R.layout.wallpaper_2);
final WallpaperManager wallpaperManager = WallpaperManager
.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
final ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setDrawingCacheEnabled(true);
imageView.setImageDrawable(wallpaperDrawable);
Button randomize = (Button) findViewById(R.id.randomize);
randomize.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
int mColor = (int) Math.floor(Math.random() * mColors.length);
wallpaperDrawable.setColorFilter(mColors[mColor],
PorterDuff.Mode.MULTIPLY);
imageView.setImageDrawable(wallpaperDrawable);
imageView.invalidate();
}
});
Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
setWallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try {
wallpaperManager.setBitmap(imageView.getDrawingCache());
finish();
} catch (IOException e) {
e.printStackTrace();
}
}
});
wallpaperwizardrii application support scrolling and not scrolling features, It means it is possible at place but after investing so much time i failed to get solution.
I tried to set wallpaperManager.setWallpaperOffsetSteps(xStep, yStep); but can not get result of it
Am I missing any api which i am not aware
Help me Thanks in advance...
EDIT:I am setting one wallpaper in my phone from below code its result is ok because im having ICS in my phone but in emulator its stretched.
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.dashboard);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(bitmap,
wallpaperManager.getDesiredMinimumWidth(),
wallpaperManager.getDesiredMinimumHeight(), true);
bitmap.recycle();
try {
wallpaperManager.setBitmap(useThisBitmap);
} catch (IOException e) {
e.printStackTrace();
}
Log.e("", "Width : " + wallpaperManager.getDesiredMinimumWidth());
Log.e("", "Height : " + wallpaperManager.getDesiredMinimumHeight());
Is there any relation with offset??
Related
I have specified an area for the imageView in the layout, in which I should display an image by referring to its path. Now, I got the image displayed on its respective imageview but
(1) the image is smaller than the allotted are for the imageView. And I want the imgae
to fit exactly the imageView
(2) the image is rotated 90 degrees anticlockwise. I want it to be
displayed normally without rotation?
Please help me to achieve the aforementioned points
Last Update
Java_Code:
private void displayMeetinPointImageInsideImageView(DataBaseRow dataBaseRow, ImageView imgSpaceIv) {
// TODO Auto-generated method stub
if (dataBaseRow == null) {
Log.w(TAG, "#displayMeetinPointImageInsideImageView(): The Object Of The DataBaseRow Class Is NULL, Maybe "
+ "It Was Not Instantiated.");
return;
} else if (imgSpaceIv == null) {
Log.w(TAG, "#displayMeetinPointImageInsideImageView(): The Variable Of Type ImageView Is NULL, Maybe It Was "
+ "Not Instantiated.");
return;
} else if (dataBaseRow.getImgPath().isEmpty()) {
Log.w(TAG, "#displayMeetinPointImageInsideImageView(): dataBaseRow.getImgPath() Is Empty");
return;
} else {
String imgPathString = dataBaseRow.getImgPath();
Options options = new Options();
options.inSampleSize = 3;
Bitmap bitmap = BitmapFactory.decodeFile(imgPathString);
Bitmap imgbitmap = BitmapFactory.decodeFile(imgPathString, options);
imgSpaceIv.setImageBitmap(imgbitmap);
}
}
Look at your logs:
dataBaseRow.getImgPath()= /storage/emulated/0/DCIM/MPL/5th guy hinging.jpeg
There is space in your filename.
I am trying to take a photo from the phones camera and place in it in a ImageButton as part of a Profile activity including all users details, and then save the image as shared pref.
If I use the following code the ImageButton simply does not update:
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
// method checks data returned form camera via startActivityForResult
super.onActivityResult(requestCode, resultCode, data);
Runnable runnable = new Runnable(){#Override
public void run() {
handler.post(new Runnable(){
#Override
public void run() {
Bundle extras = data.getExtras();
photo = (Bitmap) extras.get("data");
takeAndSetPhoto.setImageBitmap(photo);
Toast.makeText(getBaseContext(), "Image set to profile!",
Toast.LENGTH_SHORT).show();
}//edn inner run
});//end new runnab;e
}
};
new Thread(runnable).start();
}//end if result OK
}// end onActivity
Alternatively the image DOES load if I use this method but I get erros:
Allocation fail for scaled Bitmap
Out Of Memory 01-03 10:13:06.645: E/AndroidRuntime(30163):
android.view.InflateException: Binary XML file line #18: Error
inflating class
The code is using Uro for phot taken:
public void run() {
Bundle extras = data.getExtras();
Uri photoShot = data.getData(); view
takeAndSetPhoto.setImageURI(photoShot);
Toast.makeText(getBaseContext(), "Image set to profile!",
Toast.LENGTH_SHORT).show();
}//edn inner run
All suggestions appreciated.
Cheers
Ciaran
Resizing Image:
When resizing the image:
Bitmap photoBitmap = Bitmap.createScaledBitmap(photo, 100, 100, false);
It works fine on the emulator with SD card activated, but crashes on a real device (a tablet 8inch)
Problem was I was not resizing image when taken form device phone, passed image to this method before setting to the image view:
public Bitmap reSizeImage(Bitmap bitmapImage) {
// resize bitmap image passed and rerun new one
Bitmap resizedImage = null;
float factorH = h / (float) bitmapImage.getHeight();
float factorW = w / (float) bitmapImage.getWidth();
float factorToUse = (factorH > factorW) ? factorW : factorH;
try {
resizedImage = Bitmap.createScaledBitmap(bitmapImage,
(int) (bitmapImage.getWidth() * factorToUse),
(int) (bitmapImage.getHeight() * factorToUse), false);
} catch (IllegalArgumentException e) {
Log.d(TAG, "Problem resizing Image #Line 510+");
e.printStackTrace();
}
Log.d(TAG,
"in resixed, value of resized image: "
+ resizedImage.toString());
return resizedImage;
}// end reSize
I wrote an app that lets people select an image (drawable) and after they hit the "set as background" button the selected image should appear centered as the background of my phone.
Following code used to work fine. The image was placed and centered. But for operating systems (> api 13) it is depreciated. Does anyone know how to fix so that the image is also centered (or scaled to fit the screen if you wish)?
I've tried replacing it with "displaymetrics" or "points" to get w or h but it doesn't help centering the image. Is it the "suggestDesiredDimensions" that doesn't do it anymore?
ResID is the identifier of the needed drawable.
Anyone knows how to handle this? Thanks!
public void setBackground(View v)
{
try {
// Set background from a resource
WallpaperManager.getInstance(this).setResource(resID);
WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth(); // deprecated
int h = display.getHeight(); //deprecated
wm.suggestDesiredDimensions(w, h);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try using display.getSize()
public void setBackground(View v){
try {
// Set background from a resource
WallpaperManager.getInstance(this).setResource(resID);
WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int w = size.x;
int h = size.y;
wm.suggestDesiredDimensions(w, h);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I read an image from sd card with BitmapFactory:
String myJpgPath = "/sdcard/yourdollar/img001.jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options);
If I write:
Log.i("width and height: ", bm.getWidth() + " " + bm.getHeight());
I get a nullpointer exception. I tried to scale with Bitmap.createScaledBitmap() but I get the same error. After it I am processing the bitmap so I would like to have a bitmap in the end that has any width or height, because it seems like I didn't give parameters for the bitmap. But I cannot scale it, so how can I get this image as a bitmap with a width of 500 and a height of 500?
***UPDATE
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) { // <5>
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
Intent intentstart = new Intent(CameraActivity.this, Intent2.class);
startActivity(intentstart);
}
});
Okay here is the thing. This button takes a picture with the camera then change activity. If I do this way, the app does not have time to create the so when I use it in my second activity to read it, it throws nullpointerexception. So I got tricky (and wrong as well) and put:
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intentstart = new Intent(CameraActivity.this, Intent2.class);
startActivity(intentstart);
So it has 1.2 seconds to create the file, then change activity. Is there any way to check if the file created or not? Of course if its true then it should change activity.
Any suggestion?
First just check whether your image in your required directory /sdcard/yourdollar/img001.jpg And, Just try like this -
String myJpgPath = "/sdcard/yourdollar/img001.jpg";
Bitmap bm = BitmapFactory.decodeFile(myJpgPath);
and get image height & width in your Logcat
Log.i("width and height: ", bm.getWidth() + " " + bm.getHeight());
I'm building a real simple Android app and a lot is going wrong.
I can't even get a decent Try Catch Everything running. And since the machine I got is hugely underpowered I'm testing directly on my android device but that's not really working.
The application seems to crash after doing a basic action even using a generic try catch. What am I doing wrong here.
Why is the try catch not catching anything?
This is my Main.xml (a textview and another control are snipped out)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageButton
android:id="#+id/ib"
android:layout_width="300dip"
android:layout_height="50dip"
android:text="Hello World, MyActivity"
android:scaleType="fitXY"
android:onClick="onButtonClicked"
/>
This is my simple code (and the Image isn't even showing)
public void onButtonClicked(View v) {
try {
String strFilePath = "/mnt/sdcard/somefile.jpg";
if (strFilePath.length() > 0) {
File file = new File(strFilePath);
if (file.exists()) {
ShowToast(strFilePath + "Bestaat");
ImageButton image = (ImageButton) findViewById(R.id.ib);
Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
image.setImageBitmap(bMap);
ShowToast( "Done");
} else {
ShowToast(strFilePath + "Bestaat NIET");
}
} else {
ShowToast(strFilePath + "strFilePath.length() =0");
}
} catch (Exception e) {
HandleError(e);
}
}
Solution
The files where over 3Mb in size. The ImageButton won't show pictures that 'big' and simply do nothing.
The application probably threw some random out of memory exceptions loading the images taking down the entire app.
Reading a logcat was enough to debug this 'problem'
Some code that does work (kindly borrowed from other SO questions)
ImageButton image = (ImageButton) findViewById(R.id.ib);
Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
Bitmap bSized = getResizedBitmap(bMap,150,150);
image.setImageBitmap(bSized);
And
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Either you don't have an ImageButton with that id, or the image you are trying to load is to big to fit in the reduced application memory.
I have tested your code snippets..
First of all, the ImageButton will not show the android:text part because that attribute belongs to Button..
With just the LinearLayout and the ImageButton in main.xml in the res/layout folder and this code it works fine:
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onButtonClicked(View v) {
try {
String strFilePath = "/mnt/sdcard/somefile.png";
if (strFilePath.length() > 0) {
File file = new File(strFilePath);
if (file.exists()) {
//ShowToast(strFilePath + "Bestaat");
ImageButton image = (ImageButton) findViewById(R.id.ib);
Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
image.setImageBitmap(bMap);
//ShowToast( "Done");
} else {
//ShowToast(strFilePath + "Bestaat NIET");
}
} else {
//ShowToast(strFilePath + "strFilePath.length() =0");
}
} catch (Exception e) {
//HandleError(e);
e.printStackTrace();
}
}
}
So either the file is to big as K-Ballo suggested or something is wrong with your ShowToast or HandleError methods.