简体   繁体   English

从android中的图库中检索图像

[英]retrieve image from gallery in android

I have created an Activity where i have a Button .我创建了一个Activity ,其中有一个Button

By pressing the button an android Gallery opens.按下按钮会打开一个 android Gallery When i choose an image from the gallery it is shows it in an ImageView of my Activity but after choosing second time the following error occuring当我从图库中选择一个图像时,它会在我的ActivityImageView中显示它,但是在第二次选择后出现以下错误

01-13 17:55:25.323: ERROR/AndroidRuntime(14899): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

Here is the source code i am using:这是我正在使用的源代码:

public class MyImage extends Activity {
    /** Called when the activity is first created. */
    Gallery gallery;
     private Uri[] mUrls;  
    String[] mFiles=null;  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

          File images = Environment.getDataDirectory();  
                 File[] imagelist = images.listFiles();  

                  mFiles = new String[imagelist.length];  

                          for(int i= 0 ; i< imagelist.length; i++)  
                          {  
                              mFiles[i] = imagelist[i].getAbsolutePath();  
                          }  
                          mUrls = new Uri[mFiles.length];  

                          for(int i=0; i < mFiles.length; i++)  
                          {  
                              mUrls[i] = Uri.parse(mFiles[i]);     
                          }     

                          Gallery g = (Gallery) findViewById(R.id.Gallery01);  
                          g.setAdapter(new ImageAdapter(this));  
                          g.setFadingEdgeLength(40);  
    }

     public class ImageAdapter extends BaseAdapter{  

                 int mGalleryItemBackground;  
                 public ImageAdapter(Context c)  {     
                     mContext = c;     
                 }  
                 public int getCount(){  
                     return mUrls.length;  
                 }  
                 public Object getItem(int position){  
                     return position;  
                 }  
                 public long getItemId(int position) {  
                     return position;  
                 }  
                public View getView(int position, View convertView, ViewGroup parent){  
                     ImageView i = new ImageView(mContext);  

                     i.setImageURI(mUrls[position]);  
                     i.setScaleType(ImageView.ScaleType.FIT_XY);  
                     i.setLayoutParams(new Gallery.LayoutParams(260, 210));  
                     return i;  
                 }     
                 private Context mContext;  
                 }     
}

There are plenty of answer related to this.有很多与此相关的答案。 Check it out .检查一下

I have encountered this exactly same problem too.我也遇到过这个完全相同的问题。 What you can do to prevent this from happening is to make sure you recycle the previous Bitmap before loading the new one.为了防止这种情况发生,您可以做的是确保在加载新位图之前回收先前的位图。 Call Bitmap.recycle() to do this.调用 Bitmap.recycle() 来做到这一点。 http://developer.android.com/reference/android/graphics/Bitmap.html#recycle () http://developer.android.com/reference/android/graphics/Bitmap.html#recycle ()

I don't see where you handle the Bitmaps in you code but you get the idea?我没有看到您在代码中处理位图的位置,但您明白了吗?

Another thing when handling Bitmaps is to keep in ming how big they actually are when read into memory and do you really need to have that large images for your usecase.处理位图时的另一件事是记住它们在读入内存时实际有多大,并且您是否真的需要为您的用例提供那么大的图像。 You can read the Bitmap to your app with lower sample rate if you don't need to have the full image.如果您不需要完整图像,您可以以较低的采样率将位图读取到您的应用程序中。 This saves a lot of memory.这样可以节省大量内存。

It is very common problem.这是很常见的问题。 It is maybe because you are having many images, and the Android emulator heap size is small.可能是因为你有很多图像,而Android模拟器堆大小很小。 So you have to recycle your imagebitmap each time after use.所以每次使用后你都必须回收你的imagebitmap。

See this question . 看到这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM