2016-11-14 5 views
1

Ich habe vor kurzem mit Android-Entwicklung auf Studio begonnen, und versuche, eine App zu erstellen, wo ich die Kamera verwenden, um ein Bild klicken und in der Galerie dieser App anzuzeigen. Es ist ein Basisszenario, wie es heute in allen Foto-Apps der Fall ist - eine Kamera und eine Galerie, um die Bilder zu zeigen, die von dieser App selbst angeklickt wurden.Bilder, die zufällig in gridView angezeigt werden, während Android Studio

Ich habe es geschafft, die Kamera zu integrieren, und ich arbeite jetzt an der Galerie Teil. Ich habe die Rasteransicht verwendet, um das Layout der Galerieaktivität zu erstellen, aber es funktioniert nicht gut, da die Bilder ein wenig willkürlich erscheinen.

Unten ist mein Code für die "GridViewAdapter.java" -

public class GridViewAdapter extends BaseAdapter { 

    // Declare variables 
    private Activity activity; 
    private String[] filepath; 
    private String[] filename; 

    private static LayoutInflater inflater = null; 

    public GridViewAdapter(Activity a, String[] fpath, String[] fname) { 
     activity = a; 
     filepath = fpath; 
     filename = fname; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 
     return filepath.length; 

    } 

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

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     if (convertView == null) 
      vi = inflater.inflate(R.layout.grid_item_layout, null); 

     // Locate the ImageView in gridview_item.xml 
     ImageView image = (ImageView) vi.findViewById(R.id.image); 
     //image.setLayoutParams(new GridView.LayoutParams(215, 215)); 
     image.setPadding(8, 8, 8, 8); 

     // Decode the filepath with BitmapFactory followed by the position 
     Bitmap bmp = BitmapFactory.decodeFile(filepath[position]); 

     // Set the decoded bitmap into ImageView 
     image.setImageBitmap(bmp); 
     return vi; 
    } 
} 

Ich habe die image.setLayoutParams kommentiert out() Linie, weil es meine App zum Absturz verursacht.

Und das ist die entsprechende XML-Datei -

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

    <GridView 
     android:id="@+id/gallery" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:columnWidth="90dp" 
     android:numColumns="3" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:stretchMode="spacingWidthUniform" 
     android:gravity="center"/> 

</RelativeLayout> 

Die Ausgabe, die ich erhalte ist - screenShot of the app

Ich begann nur apps in dieser Woche entwickeln, so werde ich sehr dankbar für jede Hilfe hier zur Verfügung gestellt.

Vielen Dank.

EDIT

Unten finden Sie den Code für "grid_item_layout.xml" -

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

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/image" /> 

</RelativeLayout> 
+0

Bitte fügen Sie grid_item_layout.xml Quellcode auch –

+0

Done that @RahulKumar –

Antwort

0
<GridView 
    android:id="@+id/gallery" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:numColumns="3"/> 

dies versuchen.

+1

Danke @RahulKumar, das hat es einfach perfekt gemacht :) –

Verwandte Themen