【发布时间】:2014-01-28 00:42:49
【问题描述】:
我制作了一个壁纸集应用程序,在我的设备 (Nexus 5) 上效果很好,但在 Galaxy TAB、Fame 甚至 Optimus 上它会给出 java.lang.OutOfMemory 错误。 有人告诉我实现 public Bitmap decodeAndResizeFile(File f) 但我明白了
这是我的 MainActivity 代码 包 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;
}
我在这里实现了它:
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;
我在这里检索它:
文件文件=新文件(Uri.parse(String.valueOf(toPhone)));
在上面的行我得到 构造函数 File(Uri) 未定义错误
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;
}
}
}
我该如何定义它?我做错了什么?
【问题讨论】:
标签: android bitmap out-of-memory