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

  • Home
  • SEARCH
  • 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 8167855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:20:03+00:00 2026-06-06T20:20:03+00:00

I was using Google’s Hello GridView Tutorial to learn how use GridView . It

  • 0

I was using Google’s Hello GridView Tutorial to learn how use GridView. It work correctly with Drawable, I can add many items to the GridView. When running the app I can scroll and rotate the phone.

However, if I use images or photos from the SD card things go wrong. I can display the Activity with thumbnails, as with Drawable, but when I rotate the phone I get the message:

The application has stopped unexpectedly.

Can someone help me?

Manifest :

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The layout :

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

</LinearLayout>

The activity :
package test.test;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    protected String[] listImages;
    protected String txt;

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

        GridView gridview = (GridView) findViewById(R.id.gridview);

        // search files
        this.listImages = listFile("/sdcard/projetv/test");

        // to show files name
        TextView text = (TextView) findViewById(R.id.txt);
        text.setText(this.txt);

        gridview.setAdapter(new ImageAdapter(this));

    }

    /*
     * Return list of files in the directory "path", with extention "ext"
     */
    protected String[] listFile(String path) {
        File curs = new File(path);
        String[] temp = curs.list();

        this.txt = new String("");

        if (temp != null)
            for (int i = 0; i < temp.length; i++) {
                this.txt = this.txt + " | " + temp[i];
                temp[i] = path + "/" + temp[i];
            }

        return temp;
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            // return 20;
            return listImages.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) { // if it's not recycled, initialize some
                                        // attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            // load the image
            imageView.setImageBitmap(BitmapFactory
                    .decodeFile(listImages[position]));

            // load drawable
            // imageView.setImageResource(R.drawable.ic_launcher);
            return imageView;
        }
    }

}

Logcat :

06-18 07:14:33.625: D/dalvikvm(521): GC freed 687 objects / 56160 bytes in 50ms
06-18 07:14:34.744: D/dalvikvm(521): GC freed 71 objects / 35824 bytes in 44ms
06-18 07:14:35.544: D/dalvikvm(521): GC freed 139 objects / 39392 bytes in 41ms
06-18 07:14:36.344: D/dalvikvm(521): GC freed 78 objects / 68928 bytes in 44ms
06-18 07:14:39.184: D/dalvikvm(521): GC freed 179 objects / 43424 bytes in 115ms
06-18 07:14:40.194: E/dalvikvm-heap(521): 3840000-byte external allocation too large for this process.
06-18 07:14:40.194: E/(521): VM won't let us allocate 3840000 bytes
06-18 07:14:40.194: D/skia(521): --- decoder->decode returned false
06-18 07:14:40.194: D/AndroidRuntime(521): Shutting down VM
06-18 07:14:40.194: W/dalvikvm(521): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-18 07:14:40.194: E/AndroidRuntime(521): Uncaught handler: thread main exiting due to uncaught exception
06-18 07:14:40.204: E/AndroidRuntime(521): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296)
06-18 07:14:40.204: E/AndroidRuntime(521):     at test.test.TestActivity$ImageAdapter.getView(TestActivity.java:92)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.AbsListView.obtainView(AbsListView.java:1256)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.GridView.makeAndAddView(GridView.java:1218)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.GridView.makeRow(GridView.java:265)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.GridView.fillSpecific(GridView.java:465)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.GridView.layoutChildren(GridView.java:1123)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.os.Looper.loop(Looper.java:123)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-18 07:14:40.204: E/AndroidRuntime(521):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 07:14:40.204: E/AndroidRuntime(521):     at java.lang.reflect.Method.invoke(Method.java:521)
06-18 07:14:40.204: E/AndroidRuntime(521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-18 07:14:40.204: E/AndroidRuntime(521):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-18 07:14:40.204: E/AndroidRuntime(521):     at dalvik.system.NativeStart.main(Native Method)
06-18 07:14:40.224: I/dalvikvm(521): threadid=7: reacting to signal 3
06-18 07:14:40.224: E/dalvikvm(521): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

STEP II :

I try to load images in gridview, like you said Luksprog, but it dont work. I think I dont see how I can do this.

New TestActivity.java :

package test.test;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    protected String[] listImages;
    protected String txt;
    protected BitmapFactory.Options opt;
    protected GridView.LayoutParams paramgrid;

    public TestActivity() {
        super();
        // search files
        if (this.listImages == null)
            this.listImages = listFile("/sdcard/projetv/test");

        // set options of thumbnails
        this.opt = new BitmapFactory.Options();
        this.opt.inPreferredConfig = Bitmap.Config.RGB_565;
        this.opt.inSampleSize = 3;
        this.paramgrid = new GridView.LayoutParams(85, 85);
    }

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

        GridView gridview = (GridView) findViewById(R.id.gridview);

        // to show files name
        TextView text = (TextView) findViewById(R.id.txt);
        text.setText(this.txt);

        // load images in GridView
        ImageView imageView;
        for(int i=0;i<this.listImages.length;i++){
            imageView = new ImageView(this);
            imageView.setLayoutParams(this.paramgrid);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            imageView.setImageBitmap(BitmapFactory.decodeFile(this.listImages[i], this.opt));

            gridview.addView(imageView, i);
        }

    }

    /*
     * Return list of files in the directory "path", with extention "ext"
     */
    protected String[] listFile(String path) {
        File curs = new File(path);
        String[] temp = curs.list();

        this.txt = new String("");

        if (temp != null)
            for (int i = 0; i < temp.length; i++) {
                this.txt = this.txt + " | " + temp[i];
                temp[i] = path + "/" + temp[i];
            }

        return temp;
    }
}

Logcat :

06-20 07:48:50.946: D/dalvikvm(520): GC freed 773 objects / 57808 bytes in 57ms
06-20 07:48:51.466: D/AndroidRuntime(520): Shutting down VM
06-20 07:48:51.466: W/dalvikvm(520): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-20 07:48:51.466: E/AndroidRuntime(520): Uncaught handler: thread main exiting due to uncaught exception
06-20 07:48:51.466: E/AndroidRuntime(520): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.test/test.test.TestActivity}: java.lang.UnsupportedOperationException: addView(View, int) is not supported in AdapterView
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.os.Looper.loop(Looper.java:123)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-20 07:48:51.466: E/AndroidRuntime(520):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 07:48:51.466: E/AndroidRuntime(520):     at java.lang.reflect.Method.invoke(Method.java:521)
06-20 07:48:51.466: E/AndroidRuntime(520):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-20 07:48:51.466: E/AndroidRuntime(520):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-20 07:48:51.466: E/AndroidRuntime(520):     at dalvik.system.NativeStart.main(Native Method)
06-20 07:48:51.466: E/AndroidRuntime(520): Caused by: java.lang.UnsupportedOperationException: addView(View, int) is not supported in AdapterView
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.widget.AdapterView.addView(AdapterView.java:448)
06-20 07:48:51.466: E/AndroidRuntime(520):     at test.test.TestActivity.onCreate(TestActivity.java:56)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-20 07:48:51.466: E/AndroidRuntime(520):     ... 11 more
06-20 07:48:51.486: I/dalvikvm(520): threadid=7: reacting to signal 3
06-20 07:48:51.486: E/dalvikvm(520): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Have you more information to help me ? Thanks

  • 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-06-06T20:20:06+00:00Added an answer on June 6, 2026 at 8:20 pm

    Thanks to Luksprog directions, I search on the web and I found a good tutorial about this think : Displaying images from SD card in Android . It works ! I hope it can help somebody too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using google, yahoo and AOL smtp and PHPMailer how many mails can be sent
Using Google's Hello TabWidget tutorial found here I was able to build a tabWidget
Using Google Apps Script, I've made a UI for use on a Google Spreadsheet.
When using Google Website Optimizer A/B split test, is there a way I can
I'm using Google App Engine with python. And I can't install third party library.
When using Google Chrome, I want to debug some JavaScript code. How can I
Using Google Test 1.6 (Windows 7, Visual Studio C++). How can I turn off
Using google's json parser , i test file's content to see if it can
Im using Google's geocode/googlemaps api. I can succesfully geo-code a location, and place a
By using Google Web Fonts can/does Google process the text of the webpage (despite

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.