Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4020222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:12:05+00:00 2026-05-20T10:12:05+00:00

I’m loading images from my phone gallery and loading it into my app gallery,

  • 0

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.

My 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();
        }
    });
}

}

 public class ImageLoader {

    private List<String> ImageFiles = new ArrayList<String>();
    private static final String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString()+ "/DCIM/Camera";    
    private File file;

    public ImageLoader(){
        ReadImages();
    }

    public List<String> getImageFiles(){
        return ImageFiles;
    }

    private void ReadImages(){
        file = new File(CAMERA_IMAGE_BUCKET_NAME);
        File[] URIs = file.listFiles(new ImageFileFilter());
        for(int x = 0; x < 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<String> 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;
    }
}

Thanks for your help!

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]  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:12:06+00:00Added an answer on May 20, 2026 at 10:12 am

    I changed the getView method to this:

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

    And I Used a new decode method I got from link

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I used javascript for loading a picture on my website depending on which small
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.