Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有人能告诉我为什么我的android应用程序在将图像加载到gallery时会强制关闭吗_Android_Android Gallery - Fatal编程技术网

有人能告诉我为什么我的android应用程序在将图像加载到gallery时会强制关闭吗

有人能告诉我为什么我的android应用程序在将图像加载到gallery时会强制关闭吗,android,android-gallery,Android,Android Gallery,我正在从手机图库加载图像并将其加载到应用程序图库中,但每次运行应用程序时,它都会强制关闭 我的代码: public class ImageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.imagelayout); Gall

我正在从手机图库加载图像并将其加载到应用程序图库中,但每次运行应用程序时,它都会强制关闭

我的代码:

 public class ImageActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imagelayout);

    Gallery gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));


    gallery.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView parent, 
        View v, int position, long id) 
        {                
            Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();
        }
    });
}
}

公共类ImageLoader{
private List ImageFiles=new ArrayList();
私有静态最终字符串CAMERA\u IMAGE\u BUCKET\u NAME=Environment.getExternalStorageDirectory().toString()+“/DCIM/CAMERA”;
私有文件;
公共图像加载器(){
ReadImages();
}
公共列表getImageFiles(){
返回图像文件;
}
私有void ReadImages(){
文件=新文件(相机\图像\桶\名称);
File[]URIs=File.listFiles(新的ImageFileFilter());
对于(intx=0;x
谢谢你的帮助

Stack trace:
Thread [<1> main] (Suspended (exception OutOfMemoryError))  
    BitmapFactory.decodeFile(String, BitmapFactory$Options) line: 277   
    BitmapFactory.decodeFile(String) line: 296  
    ImageAdapter.getView(int, View, ViewGroup) line: 63 
    Gallery.makeAndAddView(int, int, int, boolean) line: 745    
    Gallery.fillToGalleryRight() line: 697  
    Gallery.layout(int, boolean) line: 628  
    Gallery.onLayout(boolean, int, int, int, int) line: 336 
    Gallery(View).layout(int, int, int, int) line: 7035 
    LinearLayout.setChildFrame(View, int, int, int, int) line: 1249 
    LinearLayout.layoutVertical() line: 1125    
    LinearLayout.onLayout(boolean, int, int, int, int) line: 1042   
    LinearLayout(View).layout(int, int, int, int) line: 7035    
    LinearLayout.setChildFrame(View, int, int, int, int) line: 1249 
    LinearLayout.layoutHorizontal() line: 1238  
    LinearLayout.onLayout(boolean, int, int, int, int) line: 1044   
    LinearLayout(View).layout(int, int, int, int) line: 7035    
    FrameLayout.onLayout(boolean, int, int, int, int) line: 333 
    FrameLayout(View).layout(int, int, int, int) line: 7035 
    PhoneWindow$DecorView(FrameLayout).onLayout(boolean, int, int, int, int) line: 333  
    PhoneWindow$DecorView(View).layout(int, int, int, int) line: 7035   
    ViewRoot.performTraversals() line: 1045 
    ViewRoot.handleMessage(Message) line: 1727  
    ViewRoot(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4627    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 858  
    ZygoteInit.main(String[]) line: 616 
    NativeStart.main(String[]) line: not available [native method]  
堆栈跟踪:
线程[main](挂起(异常OutOfMemoryError))
BitmapFactory.decodeFile(字符串,BitmapFactory$选项)行:277
BitmapFactory.decodeFile(字符串)行:296
ImageAdapter.getView(int、View、ViewGroup)行:63
Gallery.makeAndAddView(int,int,int,boolean)行:745
Gallery.fillToGalleryRight()行:697
画廊。布局(整数,布尔)线:628
画廊。联机布局(布尔,int,int,int,int)行:336
画廊(视图)。布局(内,内,内,内)线:7035
setChildFrame(视图,int,int,int,int)行:1249
LinearLayout.layoutVertical()行:1125
线性布局。仅布局(布尔,int,int,int,int)行:1042
线性布局(视图)。布局(int,int,int,int)线:7035
setChildFrame(视图,int,int,int,int)行:1249
LinearLayout.layoutHorizontal()行:1238
线性布局。仅布局(布尔,int,int,int,int)行:1044
线性布局(视图)。布局(int,int,int,int)线:7035
FrameLayout.onLayout(boolean,int,int,int,int)行:333
框架布局(视图)。布局(int,int,int,int)行:7035
PhoneWindow$DecorView(FrameLayout).仅限布局(boolean,int,int,int,int)行:333
PhoneWindow$DecorView(视图)。布局(int,int,int,int)行:7035
ViewRoot.performTraversals()行:1045
ViewRoot.handleMessage(消息)行:1727
ViewRoot(处理程序)。dispatchMessage(消息)行:99
Looper.loop()行:123
ActivityThread.main(字符串[])行:4627
invokenactive(Object,Object[],Class,Class[],Class,int,boolean)行:不可用[本机方法]
调用(对象,对象…)行:521
ZygoteInit$MethodAndArgsCaller.run()行:858
颧骨单位主(字符串[])行:616
NativeStart.main(字符串[])行:不可用[本机方法]

OutOfMemory错误通常是因为您试图解码压缩图像,但这样做会填满堆。一旦解压缩,智能手机上的图库图像可能会非常巨大(几MB大小)。您需要使用文件的压缩版本,或者一次只处理一部分图片。

我将getView方法更改为:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(context);
    try{
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];
        Bitmap bm = this.decodeFile(images.get(position).toString());  

        imageView.setImageBitmap(bm); 
        //bm.recycle();
        bm = null;
        System.gc();
    }
    catch(Exception e){
        Log.e("DEBUGTAG", "Remtoe Image Exception", e);
    }

    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
    imageView.setBackgroundResource(itemBackground);
    return imageView;
}
我使用了一种新的解码方法

私有位图解码文件(字符串f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//我们要扩展到的新尺寸
所需的最终int_尺寸=70;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width_tmp/2c)我们可以看到堆栈跟踪,请?我在eclipse中从哪里获得堆栈跟踪?好的,我已经添加了堆栈跟踪。我注意到上面的代码中缺少一个活动类,您如何运行此应用程序?
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(context);
    try{
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];
        Bitmap bm = this.decodeFile(images.get(position).toString());  

        imageView.setImageBitmap(bm); 
        //bm.recycle();
        bm = null;
        System.gc();
    }
    catch(Exception e){
        Log.e("DEBUGTAG", "Remtoe Image Exception", e);
    }

    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
    imageView.setBackgroundResource(itemBackground);
    return imageView;
}
private Bitmap decodeFile(String 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;
        }

        //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;
}