i made a wallpaper set application, on my device (Nexus 5) it works great but on Galaxy TAB, Fame or even on Optimus it gives java.lang.OutOfMemory error. someone told me to implement public Bitmap decodeAndResizeFile(File f) but i get
here is my MainActivity code package app.technozed.cablewallpapershd;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
ImageView display;
int toPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.gc();
toPhone = R.drawable.wal1;
display = (ImageView) findViewById(R.id.WPdisplay);
ImageView image1 = (ImageView) findViewById(R.id.WPimg1);
ImageView image2 = (ImageView) findViewById(R.id.WPimg2);
ImageView image3 = (ImageView) findViewById(R.id.WPimg3);
ImageView image4 = (ImageView) findViewById(R.id.WPimg4);
ImageView image5 = (ImageView) findViewById(R.id.WPimg5);
ImageView image6 = (ImageView) findViewById(R.id.WPimg6);
ImageView image7 = (ImageView) findViewById(R.id.WPimg7);
ImageView image8 = (ImageView) findViewById(R.id.WPimg8);
ImageView image9 = (ImageView) findViewById(R.id.WPimg9);
ImageView image10 = (ImageView) findViewById(R.id.WPimg10);
Button setWall = (Button) findViewById(R.id.BsetWall);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
image7.setOnClickListener(this);
image8.setOnClickListener(this);
image9.setOnClickListener(this);
image10.setOnClickListener(this); setWall.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
here i implemented it:
public Bitmap decodeAndResizeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.WPimg1:
display.setImageResource(R.drawable.wal1);
toPhone = R.drawable.wal1;
here i retrive it:
File file=new File(Uri.parse(String.valueOf(toPhone)));
on the line above i get The constructor File(Uri) is undefined error
Bitmap bmpp = decodeAndResizeFile(file);
display.setImageBitmap(bmpp);
break;
case R.id.WPimg2:
display.setImageResource(R.drawable.wal2);
toPhone = R.drawable.wal2;
break;
case R.id.WPimg3:
display.setImageResource(R.drawable.wal3);
toPhone = R.drawable.wal3;
break;
case R.id.WPimg4:
display.setImageResource(R.drawable.wal4);
toPhone = R.drawable.wal4;
break;
case R.id.WPimg5:
display.setImageResource(R.drawable.wal5);
toPhone = R.drawable.wal5;
break;
case R.id.WPimg6:
display.setImageResource(R.drawable.wal6);
toPhone = R.drawable.wal6;
break;
case R.id.WPimg7:
display.setImageResource(R.drawable.wal7);
toPhone = R.drawable.wal7;
break;
case R.id.WPimg8:
display.setImageResource(R.drawable.wal8);
toPhone = R.drawable.wal8;
break;
case R.id.WPimg9:
display.setImageResource(R.drawable.wal9);
toPhone = R.drawable.wal9;
break;
case R.id.WPimg10:
display.setImageResource(R.drawable.wal10);
toPhone = R.drawable.wal10;
break;
case R.id.BsetWall:
try{
WallpaperManager.getInstance(getApplicationContext()).setResource(toPhone);
Toast.makeText(getApplicationContext(), "Wallpaper was set!", Toast.LENGTH_SHORT).show();
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "No privileges!", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
how can i define it? what i'm doing wrong?