2011-10-27 107 views
0

我在我的應用程序中使用了ViewFlow示例的實現,其中我必須從SdCard中滑動圖像。隨着我使用的代碼我可以滑動圖像,但它只顯示一個,我試圖從我的SD卡特定文件夾中設置ViewFlow整個圖像,但這是我的問題。我通過從sqlite數據庫獲取的ID獲取圖像的路徑,並根據我想將這些圖像添加到視圖中。所以這是我使用的,現在它的代碼:Android VeiwFlow從SdCard中加載圖像

我Cards.class:

package com.stampii.stampii.cards; 

import java.util.ArrayList; 
import java.util.HashMap; 

import com.stampii.stampii.R; 
import com.stampii.stampii.comm.rpc.UserDatabaseHelper; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageButton; 

public class Cards extends Activity { 

    public Cursor cursor; 
    int position; 
    int indexxx; 
    Bitmap b; 
    int objectId; 
    int cardsId; 
    ArrayList<HashMap<Integer, String>> imgpaths; 
    ArrayList<Integer> ids; 
    String path; 
    int mediaType=5001; 

    private ViewFlow viewFlow; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.circle_layout); 
     UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1); 
     userDbHelper.initialize(this); 
     imgpaths = new ArrayList<HashMap<Integer, String>>(); 
     HashMap<Integer, String> hm = new HashMap<Integer, String>(); 
     ids = new ArrayList<Integer>(); 

     final int cardId = getIntent().getIntExtra("card_id",0); 
     Log.i("Card Id ","Card Id : "+cardId); 
     final int collId = getIntent().getIntExtra("collection_id",0); 
     Log.i("Collection Id ","Collection Id : "+collId); 

     position = getIntent().getIntExtra("position",0); 
     Log.i("position","position : "+position); 

     String cardSQL = "SELECT cm.cardId, cm.objectId "+ 
     "FROM cardmedias AS cm "+ 
     "INNER JOIN cards AS cd "+ 
     "ON (cm.cardId = cd.objectId) "+ 
     "WHERE cd.collectionId="+collId +" AND cm.mediaType="+mediaType; 

     Cursor cards = userDbHelper.executeSQLQuery(cardSQL); 
     if (cards.getCount() == 0) { 
      Log.i("", "No Image file"); 
      cards.close(); 
     } else if (cards.getCount() > 0) { 
      for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) { 
       cardsId = Integer.parseInt(cards.getString(cards 
         .getColumnIndex("objectId"))); 
       //Log.i("", "cards objectId : " + cardsId); 

       String path = Environment.getExternalStorageDirectory() 
         + "/.Stampii/MediaCard/" + cardsId + ".png"; 
       //Log.i("", "path : " + path); 
       ids.add(cardsId); 
       hm.put(cardsId, path); 
      } 
     } 


     String sql = "SELECT objectId FROM cardmedias WHERE cardId=" + cardId 
       + " LIMIT 1"; 
     Cursor cursor = userDbHelper.executeSQLQuery(sql); 
     if (cursor.getCount() == 0) { 
      Log.i("", "No Image file"); 
      cursor.close(); 
     } else if (cursor.getCount() > 0) { 
      cursor.moveToFirst(); 
       objectId = Integer.parseInt(cursor.getString(cursor 
         .getColumnIndex("objectId"))); 
       Log.i("", "objectId : " + objectId); 
       path = hm.get(objectId); 
       Log.i("","path : "+hm.get(objectId)); 
       Log.i("","hm size : "+hm.size()); 
     } 



     Button info = (Button) findViewById(R.id.info_button); 
     info.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(Cards.this, SingleCardInfo.class); 
       intent.putExtra("card_id", cardId); 
       intent.putExtra("collection_id", collId); 
       startActivity(intent); 
      } 
     }); 

     Button back = (Button) findViewById(R.id.back_button); 
     back.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       finish(); 
      } 
     }); 

     final ArrayList<Bitmap> images = new ArrayList<Bitmap>(); 

     for(int i=0;i<=2;i++){ 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inTempStorage = new byte[16*1024]; 
       Bitmap b = BitmapFactory.decodeFile(hm.get(objectId), options); 
       Log.i("","path : "+hm.get(objectId)); 
       images.add(b); 
     } 


     viewFlow = (ViewFlow) findViewById(R.id.viewflow); 
     viewFlow.setAdapter(new ImageAdapter(this, images),position); 


     ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button); 
     prevBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       indexxx = viewFlow.getSelectedItemPosition()-1; 
       if (indexxx>=0) { 
        viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx); 
        viewFlow.setSelectedItemPosition(indexxx); 
        Log.i("indexxx", "indexxx : " + indexxx); 
       } 
      } 
     }); 

     ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button); 
     nextBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       indexxx = viewFlow.getSelectedItemPosition()+1; 
       if (indexxx<images.size()) { 
        viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx); 
        viewFlow.setSelectedItemPosition(indexxx); 
        Log.i("indexxx", "indexxx : " + indexxx); 
       } 
      } 

     }); 

    } 
    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     viewFlow.onConfigurationChanged(newConfig); 
    } 

} 

而且我ImageAdapter類:

package com.stampii.stampii.cards; 

import java.util.ArrayList; 

import com.stampii.stampii.R; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 

public class ImageAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    private ArrayList<Bitmap> ids = new ArrayList<Bitmap>(); 
    //private Bitmap bitmap; 

    public ImageAdapter(Context context, ArrayList<Bitmap> images) { 
     mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ids = images; 
    } 

    @Override 
    public int getCount() { 
     return ids.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.image_item, null); 
     } 
     ((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ids.get(position)); 
     return convertView; 
    } 

} 

所以我的問題是如何當我擁有這些圖像的路徑時,可以將我的SdCard文件夾中的所有圖像添加到此ViewFlow示例中。我試圖加載所有這些圖片,但在某些情況下,圖片太多了,它會拋出一個outofmemoryException,這就是爲什麼我認爲只爲它們設置路徑更好。所以歡迎任何幫助或建議!

在此先感謝!

這裏是ViewFlow.class代碼:ViewFlow

回答

1

你可以嘗試這樣的事情:

public class ImageAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    private ArrayList<String> ids = new ArrayList<String>(); 
    //private Bitmap bitmap; 

    public ImageAdapter(Context context, ArrayList<String> images) { 
     mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ids = images; 
    } 

    @Override 
    public int getCount() { 
     return ids.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.image_item, null); 
     } 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inTempStorage = new byte[8*1024]; 

     Bitmap ops = BitmapFactory.decodeFile(ids.get(position), options); 

     ((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ops); 
     return convertView; 
    } 

} 

和:

public class Cards extends Activity { 

public Cursor cursor; 
int position; 
int indexxx; 
Bitmap b; 
int objectId; 
int cardsId; 
ArrayList<Integer> ids; 
String path; 
int mediaType=5001; 
ArrayList<String> images; 
int card; 

private ViewFlow viewFlow; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.circle_layout); 
    UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1); 
    userDbHelper.initialize(this); 
    //imgpaths = new ArrayList<HashMap<Integer, String>>(); 
    HashMap<Integer, String> hm = new HashMap<Integer, String>(); 
    ids = new ArrayList<Integer>(); 
    images = new ArrayList<String>(); 

    final int cardId = getIntent().getIntExtra("card_id",0); 
    Log.i("Card Id ","Card Id : "+cardId); 
    final int collId = getIntent().getIntExtra("collection_id",0); 
    Log.i("Collection Id ","Collection Id : "+collId); 

    position = getIntent().getIntExtra("position",0); 
    Log.i("position","position : "+position); 

    String cardSQL = "SELECT cm.cardId, cm.objectId "+ 
    "FROM cardmedias AS cm "+ 
    "INNER JOIN cards AS cd "+ 
    "ON (cm.cardId = cd.objectId) "+ 
    "WHERE cd.collectionId="+collId +" AND cm.mediaType="+mediaType; 

    Cursor cards = userDbHelper.executeSQLQuery(cardSQL); 
    if (cards.getCount() == 0) { 
     Log.i("", "No Image file"); 
     cards.close(); 
    } else if (cards.getCount() > 0) { 
     for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) { 
      cardsId = Integer.parseInt(cards.getString(cards 
        .getColumnIndex("objectId"))); 

      card = Integer.parseInt(cards.getString(cards 
        .getColumnIndex("cardId"))); 

      String path = Environment.getExternalStorageDirectory() 
        + "/.Stampii/MediaCard/" + cardsId + ".png"; 
      ids.add(card); 
      hm.put(cardsId, path); 

      path = hm.get(cardsId); 
      Log.i("","path : "+hm.get(cardsId)); 
      Log.i("","hm size : "+hm.size()); 
      images.add(path); 
     } 
    } 


    Button back = (Button) findViewById(R.id.back_button); 
    back.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      finish(); 
     } 
    }); 


    viewFlow = (ViewFlow) findViewById(R.id.viewflow); 
    viewFlow.setAdapter(new ImageAdapter(this, images),position); 


    ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button); 
    prevBtn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      indexxx = viewFlow.getSelectedItemPosition()-1; 
      if (indexxx>=0) { 
       viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx); 
       viewFlow.setSelectedItemPosition(indexxx); 
       Log.i("indexxx", "indexxx : " + indexxx); 
      } 
     } 
    }); 

    ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button); 
    nextBtn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      indexxx = viewFlow.getSelectedItemPosition()+1; 
      if (indexxx<images.size()) { 
       viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx); 
       viewFlow.setSelectedItemPosition(indexxx); 
       Log.i("indexxx", "indexxx : " + indexxx); 
      } 
     } 
    }); 


    Button info = (Button) findViewById(R.id.info_button); 
    info.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(Cards.this, SingleCardInfo.class); 
      intent.putExtra("card_id", ids.get(position)); 
      intent.putExtra("collection_id", collId); 
      startActivity(intent); 
     } 
    }); 

} 
@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    viewFlow.onConfigurationChanged(newConfig); 
} 

}

這應該做的伎倆!