我正在做什么 ::我正在尝试从drawable加载图像并在适配器中设置为relativelayout的背景
发生了什么 ::
AdptAtomicGodGallery.java
public class AdptAtomicGodGallery extends PagerAdapter {
// Declare Variables
Context context;
Integer[] godImages;
LayoutInflater inflater;
String godTag;
public AdptAtomicGodGallery(Context context, Integer[] _godImages, String _godTag) {
this.context = context;
this.godImages = _godImages;
this.godTag=_godTag;
}
@Override
public int getCount() {
return godImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
ImageView imgGodId;
RelativeLayout relImgId;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.adpt_atomic_god_gallery, container,false);
// Locate the ImageView in viewpager_item.xml
imgGodId = (ImageView) itemView.findViewById(R.id.imgGodId);
relImgId = (RelativeLayout) itemView.findViewById(R.id.relImgId);
/*Bitmap icon = BitmapFactory.decodeResource(context.getResources(),godImages[position]);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(icon, 100, 100, false);
Drawable drawable = new BitmapDrawable(context.getResources(),resizedBitmap);
relImgId.setBackgroundDrawable(drawable);
relImgId.setTag(imgGodId);
*/
try {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),godImages[position]);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
icon.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(bs,null,options);
Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
relImgId.setBackgroundDrawable(drawable);
relImgId.setTag(imgGodId);
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((LinearLayout) object);
}
}
日志:
02-15 00:04:42.402: E/AndroidRuntime(2616): java.lang.OutOfMemoryError
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:452)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:482)
02-15 00:04:42.402: E/AndroidRuntime(2616): at com.proj.adapters.AdptAtomicGodGallery.instantiateItem(AdptAtomicGodGallery.java:73)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:800)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.support.v4.view.ViewPager.populate(ViewPager.java:991)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-15 00:04:42.402: E/AndroidRuntime(2616): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.View.measure(View.java:16540)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.Choreographer.doFrame(Choreographer.java:544)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.os.Handler.handleCallback(Handler.java:733)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.os.Handler.dispatchMessage(Handler.java:95)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.os.Looper.loop(Looper.java:136)
02-15 00:04:42.402: E/AndroidRuntime(2616): at android.app.ActivityThread.main(ActivityThread.java:5086)
02-15 00:04:42.402: E/AndroidRuntime(2616): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 00:04:42.402: E/AndroidRuntime(2616): at java.lang.reflect.Method.invoke(Method.java:515)
02-15 00:04:42.402: E/AndroidRuntime(2616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-15 00:04:42.402: E/AndroidRuntime(2616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-15 00:04:42.402: E/AndroidRuntime(2616): at dalvik.system.NativeStart.main(Native Method)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position]);
Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
relImgId.setBackgroundDrawable(drawable);
relImgId.setTag(imgGodId);
答案 0 :(得分:2)
你似乎做了很多不必要的解码。
这里正在创建第一张图片:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),godImages[position]);
这里以原始字节的形式创建第二个图像(图像的大小仍然相同):
ByteArrayOutputStream bos = new ByteArrayOutputStream();
icon.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
此处正在创建第三张图片:
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(bs,null,options);
你应该做的只是这样:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position], options);
relImgId.setImageBitmap(preview_bitmap);
在后台,ImageView实际上将创建一个BitmapDrawable
,它由您提供的位图支持。您只需解码位图一次。
答案 1 :(得分:0)
您需要缩小图像。
尝试使用此功能进行缩放:
public Bitmap decodeFile(String pathOfImage){
File f = new File(path);
if (!f.exists()){
Log.d("Reh", "File doesn't exist");
}
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 scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
然后简单地说:
imageView.setImageBitmap(decodeFile("/sdcard/whatever your folder or file name is");
或者您可以使用:getAssets()。open(" ...")从assets文件夹中获取图像。 您可能希望使用相应的解码方法。