Note that there are some explanatory texts on larger screens.

plurals
  1. POCan someone tell why my android app is force closing when loading images into gallery
    text
    copied!<p>I'm loading images from my phone gallery and loading it into my app gallery, but each time I run the app it force closes.</p> <p>My code:</p> <pre><code> 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(); } }); } </code></pre> <p>}</p> <pre><code> public class ImageLoader { private List&lt;String&gt; ImageFiles = new ArrayList&lt;String&gt;(); private static final String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString()+ "/DCIM/Camera"; private File file; public ImageLoader(){ ReadImages(); } public List&lt;String&gt; getImageFiles(){ return ImageFiles; } private void ReadImages(){ file = new File(CAMERA_IMAGE_BUCKET_NAME); File[] URIs = file.listFiles(new ImageFileFilter()); for(int x = 0; x &lt; URIs.length; x++){ File file = URIs[x]; ImageFiles.add(file.getPath()); } } /** * Image File Filter Class */ public class ImageFileFilter implements FileFilter{ private final String[] AndroidExtensions = new String[] {"jpg", "png"}; public boolean accept(File pathname) { for (String extension : AndroidExtensions) { if (pathname.getName().toLowerCase().endsWith(extension)) { return true; } } return false; } } } public class ImageAdapter extends BaseAdapter { private Context context; private int itemBackground; List&lt;String&gt; images; ImageLoader loader; public ImageAdapter(Context c){ loader = new ImageLoader(); images = loader.getImageFiles(); context = c; //---setting the style--- TypedArray a = c.obtainStyledAttributes(R.styleable.gallery); itemBackground = a.getResourceId( R.styleable.gallery_android_galleryItemBackground, 0); a.recycle(); } @Override public int getCount() { // TODO Auto-generated method stub return images.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } //---returns an ImageView view--- public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); try{ Bitmap bm = BitmapFactory.decodeFile(images.get(position).toString()); imageView.setImageBitmap(bm); } 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; } } </code></pre> <p>Thanks for your help!</p> <pre><code>Stack trace: Thread [&lt;1&gt; 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] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload