HELP: GridView Photos from SD

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

HELP: GridView Photos from SD

Postby AndNewbie » Thu Jul 15, 2010 5:02 pm

Hi,

I am a Android and also Java Newbie... I have been working through all the available tutorials and some books lately. Now, I am a little bit stuck at this example - the Grid View:
Code: Select all
public class ImageAdapter extends BaseAdapter {
    private Context mContext;

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

    public int getCount() {
        return mThumbIds.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;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}


Instead of having an array I would rather like to retrieve the images from the SD card. I have already set up an emulator with an SD and pushed several photos on it, but I really don't know how I can access it to get the images.

If someone could help me out, that would be fantastic.
AndNewbie :)
AndNewbie
Freshman
Freshman
 
Posts: 2
Joined: Thu Jul 15, 2010 4:56 pm

Top

Re: HELP: GridView Photos from SD

Postby Rollsroyc3 » Fri Jul 16, 2010 6:10 pm

Main.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<ImageView
  android:id="@+id/imageview"
  android:layout_gravity="center"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:scaleType="center"
  />
</LinearLayout>


AndroidBitmap.java
Code: Select all
package com.exercise.AndroidBitmap;

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

public class AndroidBitmap extends Activity {

private final String imageInSD = "/sdcard/er.PNG";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
   
      Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
      ImageView myImageView = (ImageView)findViewById(R.id.imageview);
      myImageView.setImageBitmap(bitmap);

  }
}






SOURCE: http://android-er.blogspot.com/2010/07/ ... -card.html
Rollsroyc3
Freshman
Freshman
 
Posts: 3
Joined: Fri Jul 16, 2010 6:05 pm

Re: HELP: GridView Photos from SD

Postby noapte » Fri Jul 23, 2010 9:48 am

Hi Rollsroyc3!

In this example you provide the exact path to an image and load that one ( imageInSD = "/sdcard/er.PNG"; ).
But what if you want to load all the images in a certain folder, for example the default images in an adroid phones, that are displayed in the build in gallery. So you dont know how many images there are, you must load them dynamically. Any clue on how to do that?
noapte
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jul 23, 2010 9:44 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests