Here in my code, I have used a Uri method to get the list of file in
my sdcard and show them by gallery and imageswitcher. However, when i
test it with my HTC wildfire s, my log tell me that the heap size is
not enough. However, when i try on the emulator and my friend's galaxy
sII, there is no problem. one more point, I have used some pictures
with around 100kB, even in Wildfire s runs smooth but when the file is
larger than around 800kB, the program was forced to stop. Galaxy SII
can load image for even 2MB. How can I solve this problem?? here is my
code. Thank You.

Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/gallery" />

    <Gallery android:id="@+id/gallery"
        android:background="#55000000"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"

        android:gravity="center_vertical"
        android:spacing="16dp" android:layout_height="100dp"/>
</RelativeLayout>


Source code:

package testimage.android;

import android.app.Activity;

import java.io.File;
import java.util.ArrayList;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class Testimage3Activity extends Activity implements
ViewFactory {

        private ImageSwitcher is;
        private Gallery gallery;
        private int downX, upX;
        private ArrayList<Uri> imgList = new ArrayList<Uri>();


        /** Called when the activity is first created. */

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

                File imgDir = new
File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
+"/image");
                if(!imgDir.exists()){
                        imgDir.mkdirs();
                }

                File[] imgFiles = imgDir.listFiles();
                Uri fileUri = null;
                if (imgFiles != null) {
                        for (File file : imgFiles) {


                                fileUri = Uri.parse("file://" + 
file.getAbsolutePath());
                                imgList.add(fileUri);

                        }

                }




                is = (ImageSwitcher) findViewById(R.id.switcher);
                is.setFactory(this);
                is.setInAnimation(AnimationUtils.loadAnimation(this,
                                android.R.anim.fade_in));
                is.setOutAnimation(AnimationUtils.loadAnimation(this,
                                android.R.anim.fade_out));
                is.setOnTouchListener(new OnTouchListener() {

                        public boolean onTouch(View v, MotionEvent event) {
                                if (event.getAction() == 
MotionEvent.ACTION_DOWN) {
                                        downX = (int) event.getX();
                                        return true;
                                } else if (event.getAction() == 
MotionEvent.ACTION_UP) {
                                        upX = (int) event.getX();
                                        int index = 0;
                                        if (upX - downX > 100) {

                                                if 
(gallery.getSelectedItemPosition() == 0)
                                                        index = 
gallery.getCount() - 1;
                                                else
                                                        index = 
gallery.getSelectedItemPosition() - 1;
                                        } else if (downX - upX > 100) {

                                                if 
(gallery.getSelectedItemPosition() == (gallery
                                                                .getCount() - 
1))
                                                        index = 0;
                                                else
                                                        index = 
gallery.getSelectedItemPosition() + 1;
                                        }

                                        gallery.setSelection(index, true);
                                        return true;
                                }
                                return false;
                        }

                });

                gallery = (Gallery) findViewById(R.id.gallery);
                gallery.setAdapter(new ImageAdapter(this));
                gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
                        public void onItemSelected(AdapterView<?> arg0, View 
arg1,
                                        int position, long arg3) {
                                is.setImageURI(imgList.get(position));
                        }

                        public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub
                        }

                });
        }
        public View makeView() {

                ImageView i = new ImageView(this);
                i.setBackgroundColor(0xFF000000);
                i.setScaleType(ImageView.ScaleType.CENTER);
                i.setLayoutParams(new ImageSwitcher.LayoutParams(
                                LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT));
                return i;
        }

        public class ImageAdapter extends BaseAdapter {
                public ImageAdapter(Context c) {
                        mContext = c;
                }

                public int getCount() {
                        return imgList.size();
                }

                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(imgList.get(position));
                        i.setAdjustViewBounds(true);
                        i.setLayoutParams(new Gallery.LayoutParams(
                                        50,70));
                        return i;}

                private Context mContext;
        }


}



Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
    package="testimage.android"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

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

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to