2012-03-25 3 views
0

Ich versuche, Galerie mit Fotos von SD-Karte zu erstellen. Ich habe eine Datenbanktabelle. Eine der Spalten in der Tabelle enthält einen Pfad zu einem Foto.Android - Galerie mit Fotos von SD-Karte erstellen

Ich folge diesem Link als Richtlinie, aber dieser Beispielcode ruft Bild-IDs aus Res/Drawable-Verzeichnis. Ich bin mir nicht sicher, wie ich es modifizieren soll, damit mein Code funktioniert.

http://saigeethamn.blogspot.com/2010/05/gallery-view-android-developer-tutorial.html

Aber wie bereits erwähnt, möchte ich Fotos angezeigt werden mit dem, was Bilder gibt es in der Datenbanktabelle. Der folgende Code ist ImageAdapter.

public class ImageAdapter extends BaseAdapter { 

    private Context ctx; 

    int imageBackground; 

    public ImageAdapter(Context c) { 
     ctx = c; 
     TypedArray ta = obtainStyledAttributes(R.styleable.milestone_style); 
     imageBackground = ta.getResourceId(R.styleable.milestone_style_android_galleryItemBackground, 1); 
     ta.recycle(); 
    } 

    @Override 
    public int getCount() { 
     return imagePhotosLocations.length; 
    } 

    @Override 
    public Object getItem(int arg0) { 

     return arg0; 
    } 

    @Override 
    public long getItemId(int arg0) { 

     return arg0; 
    } 

    @Override 
    public View getView(int position, View arg1, ViewGroup arg2) { 
     if (milestones != null && !milestones.isEmpty()) { 
      Milestone milestone = milestones.get(0); 
      String imageFileLocation = milestone.getFileLocation(); 
      System.out.println("imageFileLocation="+imageFileLocation); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 26; 
      Bitmap b; 
      ImageView iv = new ImageView(ctx); 
      try { 
       if (imageFileLocation.startsWith("content://")) { 
        b = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(imageFileLocation)), null, options); 
       } else { 
        b = BitmapFactory.decodeFile(imageFileLocation, options); 
       } 
       iv.setImageBitmap(b); 
       iv.setScaleType(ImageView.ScaleType.FIT_XY); 
       iv.setLayoutParams(new Gallery.LayoutParams(150, 120)); 
       // milestoneImageView.setBackgroundResource(imageBackground); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 

      return iv; 
     } 
     return null; 
    } 
} 

Und dies ist eine der Methoden in Aktivität.

private String[] imagePhotosLocations; 

private void init() { 
    milestones = getMilestones(); 
    imagePhotosLocations = new String[milestones.size()]; 
    int index = 0; 
    for (Milestone milestone : milestones) { 
     imagePhotosLocations[index++] = milestone.getFileLocation(); 
    } 
} 

private void initializeGallery() { 
    milestoneImageView = (ImageView) this.findViewById(R.id.imagePhoto); 
    Gallery milestoneGallery = (Gallery) this.findViewById(R.id.milestoneGallery); 
    if (milestoneGallery == null) { 
     throw new IllegalArgumentException("milestoneGallery can not be null."); 
    } 
    milestoneGallery.setAdapter(new ImageAdapter(this)); 
    milestoneGallery.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int which, long arg3) { 
      String imageFileLocation = imagePhotosLocations[which]; 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 26; 
      Bitmap b; 
      try { 
       if (imageFileLocation.startsWith("content://")) { 
        b = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(imageFileLocation)), null, options); 
       } else { 
        b = BitmapFactory.decodeFile(imageFileLocation, options); 
       } 
       milestoneImageView.setImageBitmap(b); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

Antwort

1

Hier sind die Schritte, die ich verwendet,

  1. Prüfen Sie zuerst, ob die SD-Karte installiert ist.
  2. Abfrage Ihrer Datenbank.Details finden Sie hier:
    SQLite tutorial.
  3. Die Abfrage gibt einen Cursor für die Ergebnismenge zurück.
  4. Verwenden Sie einen benutzerdefinierten Cursor-Adapter (Siehe: Custom Cursor Adapter)
  5. Einige Male, wenn das Bild zu groß ist, Budget, das Bild in Bitmap-Laden nicht überschreiten VM. So siehe diesen Artikel Avoiding out of memory issue while loading image

  6. So der Cursor-Adapter gibt eine Bildansicht zurück, wie es in Ihrem Code getan wird.

  7. schließlich binden Sie diesen benutzerdefinierten Adapter an Ihre Gallary.

hoffe dies beantwortet Ihre Frage.

+0

Danke. Ich werde es versuchen. – user826323