views:

34

answers:

0

Hello everyone, I wanna try an example of storing images into sdcard and displaying out to see if it works. However, my images can't be shown. Please help me check my problem.

public class SDCard_Image {

//read drawable files 
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.mood_1);

//insert into sd card(?) 
 public static boolean StoreByteImage(Context mContext, byte[] imageData,
            int quality, String expName) { 

        File sdImageMainDirectory = new File("/sdcard/myImages");

        FileOutputStream fileOutputStream = null;       
        FileOutputStream fileOutputStream2 = null;
        FileOutputStream fileOutputStream3 = null;
        FileOutputStream fileOutputStream4 = null;
        FileOutputStream fileOutputStream5 = null;
        FileOutputStream fileOutputStream6 = null;
        FileOutputStream fileOutputStream7 = null;
        FileOutputStream fileOutputStream8 = null;
        FileOutputStream fileOutputStream9 = null;
        FileOutputStream fileOutputStream10 = null;
        FileOutputStream fileOutputStream11 = null;
        FileOutputStream fileOutputStream12 = null;
        FileOutputStream fileOutputStream13 = null;
        FileOutputStream fileOutputStream14 = null;
        FileOutputStream fileOutputStream15 = null;
        FileOutputStream fileOutputStream16 = null;

        String mood_1 = null;
        String mood_2 = null; 
        String mood_3 = null;
        String mood_4 = null; 
        String mood_5 = null;
        String mood_6 = null; 
        String mood_7 = null;
        String mood_8 = null; 
        String mood_9 = null; 
        String mood_10 = null; 
        String mood_11 = null; 
        String mood_12 = null; 
        String mood_13 = null; 
        String mood_14 = null; 
        String mood_15 = null; 
        String mood_16 = null; 

        try {

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 5;

            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length,options);

            fileOutputStream = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_1 + ".jpg");
            fileOutputStream2 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_2 + ".jpg");
            fileOutputStream3 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_3 + ".jpg");
            fileOutputStream4 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_4 + ".jpg");
            fileOutputStream5 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_5 + ".jpg");
            fileOutputStream6 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_6 + ".jpg");
            fileOutputStream7 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_7 + ".jpg");
            fileOutputStream8 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_8 + ".jpg");
            fileOutputStream9 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_9 + ".jpg");
            fileOutputStream10 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_10 + ".jpg");
            fileOutputStream11 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_11 + ".jpg");
            fileOutputStream12 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_12 + ".jpg");
            fileOutputStream13 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_13 + ".jpg");
            fileOutputStream14 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_14 + ".jpg");
            fileOutputStream15 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_15 + ".jpg");
            fileOutputStream16 = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/" + mood_16 + ".jpg");

            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);

            myImage.compress(CompressFormat.JPEG, quality, bos);

            bos.flush();
            bos.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return true;
    }


private Resources getResources() {
    // TODO Auto-generated method stub
    return null;
}

}

And this is the part whereby i wanna show my images:

public class SDCardImagesActivity extends Activity {

private Cursor cursor;
private int columnIndex;
private DBAdapter db; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mood);
    db = new DBAdapter(this); 
    db.open();

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);

    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView sdcardImages = (GridView) findViewById(R.id.gridview);
    sdcardImages.setAdapter(new ImageAdapter(this));

    // Set up a click listener
    sdcardImages.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            // Get the data location of the image
            String[] projection = {MediaStore.Images.Media.DATA};
            cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, // Which columns to return
                    null,       // Return all rows
                    null,
                    null);
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToPosition(position);
            // Get image filename
            String imagePath = cursor.getString(columnIndex);
            // Use this path to do further processing, i.e. full screen display
        }
    });
}

private class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() {
        return cursor.getCount();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(8, 8, 8, 8);
            picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}

}

Sorry the code is really long, but thanks!