IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

comment afficher un video gallery


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Décembre 2008
    Messages
    233
    Détails du profil
    Informations personnelles :
    Âge : 38

    Informations forums :
    Inscription : Décembre 2008
    Messages : 233
    Par défaut comment afficher un video gallery
    salut à tous,
    grâce à deux tuto j'ai reussi à afficher une gallerie de photo chargée à partir de mon SDcard, mais en fait je veux plus précisamment afficher une gallerie de vidéo (aussi se trouvant dans la SDcard)
    d'abord est ce que c'est faisable sous android 2.1?
    si c'est le cas,quelles sont les modifications que je dois affecter à mon ancien code?
    voici le code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    package com.pfe.galleryExample;
     
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import com.pfe.galleryExample.R;
    import android.app.Activity;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.AdapterView.OnItemClickListener;
     
    public class GalleryExample extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) { 
            super.onCreate(icicle); 
            setContentView(R.layout.main); 
     
            /* Find the gallery defined in the main.xml 
             * Apply a new (custom) ImageAdapter to it. */ 
            Gallery g = (Gallery) findViewById(R.id.gallery) ;
                   g.setAdapter(new ImageAdapter(this, ReadSDCard())); 
     
                   g.setOnItemClickListener(new OnItemClickListener() {   
                       public void onItemClick(AdapterView<?> parent,   
                         View v, int position, long id) {   
                       }   
                   });
        }
     
     
            private List<String> ReadSDCard()   
            {   
             List<String> tFileList = new ArrayList<String>();   
     
             //It have to be matched with the directory in SDCard   
             File f = new File("/sdcard");   
     
             File[] files=f.listFiles();   
     
             for(int i=0; i<files.length; i++)   
             {   
              File file = files[i];   
              /*It's assumed that all file in the path  
                are in supported type*/   
              tFileList.add(file.getPath());   
             }   
     
             return tFileList; 
     
     
     
        }
     
        public class ImageAdapter extends BaseAdapter { 
            /** The parent context */ 
               private Context myContext; 
     
               /** All images to be displayed. 
                * Put some images to project-folder: 
                * '/res/drawable/uvw.xyz' .*/ 
               /*private int[] myImageIds = { 
                      R.drawable.image_1, 
                      R.drawable.image_2, 
                      R.drawable.image_3, 
                      R.drawable.image_4 
               }; */
     
               int mGalleryItemBackground;   
               //private Context mContext;   
               private List<String> FileList;
     
     
     
               /** Simple Constructor saving the 'parent' context. */ 
               public ImageAdapter(Context c, List<String> fList) {
            	   this.myContext = c; 
     
            	   FileList = fList;   
                   TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);   
                   mGalleryItemBackground = a.getResourceId(   
                 R.styleable.Gallery1_android_galleryItemBackground,0);   
                   a.recycle(); 
     
               } 
     
               /** Returns the amount of images we have defined. */ 
               //public int getCount() { return this.myImageIds.length; } 
               public int getCount() { return FileList.size();  }   
     
               /* Use the array-Positions as unique IDs */ 
               public Object getItem(int position) { return position; } 
               public long getItemId(int position) { return position; }
     
     
               /** Returns a new ImageView to 
                * be displayed, depending on 
                * the position passed. */ 
               public View getView(int position, View convertView, ViewGroup parent) { 
                   /*ImageView i = new ImageView(this.myContext); 
     
                   i.setImageResource(this.myImageIds[position]); 
                   /* Image should be scaled as width/height are set. */ 
                  // i.setScaleType(ImageView.ScaleType.FIT_XY); 
                   /* Set the Width/Height of the ImageView. */ 
                  /* i.setLayoutParams(new Gallery.LayoutParams(150, 150)); 
                   return i; */
            	   ImageView i = new ImageView(myContext);   
     
                   Bitmap bm = BitmapFactory.decodeFile(   
                     FileList.get(position).toString());   
                   i.setImageBitmap(bm);   
     
                   i.setLayoutParams(new Gallery.LayoutParams(150, 100));   
                   i.setScaleType(ImageView.ScaleType.FIT_XY);   
                   i.setBackgroundResource(mGalleryItemBackground);   
     
                   return i; 
     
               } 
     
               ////
               public TypedArray obtainStyledAttributes(int theme) {   
            	    // TODO Auto-generated method stub   
            	    return null;   
            	}   
               ////
     
               /** Returns the size (0.0f to 1.0f) of the views 
                * depending on the 'offset' to the center. */ 
               public float getScale(boolean focused, int offset) { 
                 /* Formula: 1 / (2 ^ offset) */ 
                   return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
               }
        }    
    }

    merciiiiiiiiiiiiiiiiiiiiiiii d'avance

  2. #2
    Membre confirmé
    Inscrit en
    Décembre 2008
    Messages
    233
    Détails du profil
    Informations personnelles :
    Âge : 38

    Informations forums :
    Inscription : Décembre 2008
    Messages : 233
    Par défaut
    re!
    bon pour creer une gallerie video il faut utiliser l'API
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    import android.provider.MediaStore.Video.Thumbnails ;
    aussi il faut utiliser des curseurs puisque MediaStore est la BD ou le Smartphone stocke ses données média
    j'ai trouvé un tutorial qui explique comment creer image gallery en utilisant les curseurs (autrement 2eme methode que celle indiqué dans le poste précédent )mais pour video gallery y 'a pas encore de tuto vu que android.provider.MediaStore.Video.Thumbnails est supporté a partir de android 2.0
    voici le code de image gallery
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    package image.Thumbnails;
     
    import  image.Thumbnails.R;
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.AdapterView.OnItemClickListener;
    //
    import android.provider.MediaStore.Video.Thumbnails ;
     
    public class ImageThumbnailsActivity extends Activity {
          /** Called when the activity is first created. */
          private Cursor imagecursor, actualimagecursor;
          private int image_column_index, actual_image_column_index;
          GridView imagegrid;
          private int count;
          @Override
          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                init_phone_image_grid();
          }
     
     
          private void init_phone_image_grid() {
     
               /* String[] img = { MediaStore.Video.Thumbnails._ID };
                imagecursor = managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, img, null, null, MediaStore.Video.Thumbnails.VIDEO_ID + "");
                image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails._ID);
                count = imagecursor.getCount(); 
                imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
                imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
                */
        	  String[] img = { MediaStore.Video.Thumbnails._ID };
        	  ContentResolver cr = getContentResolver(); 
        	  imagecursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, img, null, null, null); 
        	  if (imagecursor.moveToNext()) { 
        		     int id = imagecursor.getInt(0); 
        		     Bitmap b = MediaStore.Video.Thumbnails.getThumbnail(cr, id, MediaStore.Video.Thumbnails.MINI_KIND, null); 
        		     //Log.d(TAG, "onCreate bitmap " + b); 
        		     ImageView iv = (ImageView) findViewById(R.id.thumbnail); 
        		     iv.setImageBitmap(b); 
     
     
                iv.setOnClickListener(new OnClickListener()  {
                      @SuppressWarnings("unchecked")
     
     
     
     
     
                      public void onClick(AdapterView parent, View v, int position, long id) {
                            System.gc();
                            String[] proj = { MediaStore.Video.Media.DATA };
                            actualimagecursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
                            actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                            actualimagecursor.moveToPosition(position);
                            String i = actualimagecursor.getString(actual_image_column_index);
                            System.gc();
                            Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                            intent.putExtra("filename", i);
                            startActivity(intent);
                      }
     
    				@Override
    				public void onClick(View v) {
    					// TODO Auto-generated method stub
     
    				}
                });
     
        	  }
     
          class ImageAdapter extends BaseAdapter {
     
        	  private Context mContext;
                public ImageAdapter(Context c) {mContext = c;}
                public int getCount() {return count;}
                public Object getItem(int position) {return position;}
                public long getItemId(int position) {return position;}
     
     
                public View getView(int position,View convertView,ViewGroup parent) {
                      System.gc();
                      ImageView i = new ImageView(mContext.getApplicationContext());
                      if (convertView == null) {
                            imagecursor.moveToPosition(position);
                            int id = imagecursor.getInt(image_column_index);
                            i.setImageURI(Uri.withAppendedPath(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, "" + id));
                            i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                            i.setLayoutParams(new GridView.LayoutParams(92, 92));
                      }
                      else {
                            i = (ImageView) convertView;
                      }
                      return i;
                }
          }
     
    }}
    ce code m'affiche un thumbnail d'un seul vidéo pourtant j'ai trois vidéo dans la SDcard de plus quand je clique la dessus il ne se selctionne meme pas


    pouvez vous le corriger? y'a un truc qui cloche

  3. #3
    Membre confirmé
    Inscrit en
    Décembre 2008
    Messages
    233
    Détails du profil
    Informations personnelles :
    Âge : 38

    Informations forums :
    Inscription : Décembre 2008
    Messages : 233
    Par défaut
    toujours la gallère !
    j'ai un problème dans la ligne en rouge, j'arrive pas à résoudre pourtant dans le Log error n'affiche aucune erreur
    SVP concentrez vous à la ligne en rouge
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    package image.Thumbnails;
    
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.res.TypedArray;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.AdapterView.OnItemClickListener;
    
    public class ImageThumbnailsActivity extends Activity  {
          /** Called when the activity is first created. */
          private Cursor imagecursor, actualimagecursor;
          private int image_column_index, actual_image_column_index;
         
          private int count;
          @Override
          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                Gallery gal = (Gallery) findViewById(R.id.gallery) ;
                gal.setAdapter(new ImageAdapter(this)); 
                //gal.setOnItemClickListener(new OnItemClickListener() );
                //gal.setOnItemClickListener(new OnItemClickListener() {   
                  //  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {   
                    //}   
               // });
            
                init_phone_image_grid();
        
          }
          
          
          private void init_phone_image_grid() {
    
               /* String[] img = { MediaStore.Video.Thumbnails._ID };
                imagecursor = managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, img, null, null, MediaStore.Video.Thumbnails.VIDEO_ID + "");
                image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails._ID);
                count = imagecursor.getCount(); 
                imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
                imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
                */
        	  String[] img = { MediaStore.Video.Thumbnails._ID,
        			  //MediaStore.Video.VideoColumns.TITLE,	//
        			  //MediaStore.Video.VideoColumns.DURATION //
        	  };
        	  //String selection = MediaStore.Video.VideoColumns.TITLE + " = \"" + OUR_TITLE + "\""; // Our images
        	  ContentResolver cr = getContentResolver(); 
        	  imagecursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, img, null, null, null); 
        	  if (imagecursor.moveToNext()) { 
        		     int id = imagecursor.getInt(0); 
        		     Bitmap b = MediaStore.Video.Thumbnails.getThumbnail(cr, id, MediaStore.Video.Thumbnails.MINI_KIND, null); 
        		    
        		     ImageView iv = (ImageView) findViewById(R.id.thumbnail);
        		     
        		     iv.setImageBitmap(b); 
        	  //}  
        	  //imagecursor.close();
        		     
        		     iv.setOnItemClickListener(new OnItemClickListener()  {
                     
    				
              
    				public void OnItemClick(AdapterView<?> parent, View v, int position, long id) {
                            System.gc();
                            String[] proj = { MediaStore.Video.Media.DATA };
                            actualimagecursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
                            actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                            actualimagecursor.moveToPosition(position);
                            String i = actualimagecursor.getString(actual_image_column_index);
                            System.gc();
                            Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                            intent.putExtra("filename", i);
                            startActivity(intent);
                      }
    
    				/*public void onClick(View v) {
    					// TODO Auto-generated method stub
    					
    				}*/
                });
                
        	  } 
        	  imagecursor.close();
        	
        	  }
    
          
          
     public class ImageAdapter extends BaseAdapter {
                
        	  private Context mContext;
        	  int mGalleryItemBackground;  
        	  
        	  public ImageAdapter(Context c) {
           	   this.mContext = c; 
              
           	  // FileList = fList;   
                  TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery);   
                  mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground,0);   
                  a.recycle(); 
              
              } 
        	  
               // public ImageAdapter(Context c) {mContext = c;}
                public int getCount() {return count;}
                public Object getItem(int position) {return position;}
                public long getItemId(int position) {return position;}
                
                
               
                public View getView(int position,View convertView,ViewGroup parent) {
                      System.gc();
                      //Log.i(TAG, "Get view = " + position); ////////
                      ImageView i = new ImageView(mContext.getApplicationContext());
                      imagecursor.requery();
                      
                      if (convertView == null) {
                            imagecursor.moveToPosition(position);
                            int id = imagecursor.getInt(image_column_index);
                            i.setImageURI(Uri.withAppendedPath(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, "" + id));
                            i.setLayoutParams(new Gallery.LayoutParams(150, 100)); //
                            i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                            //i.setLayoutParams(new GridView.LayoutParams(92, 92));
                            i.setBackgroundResource(mGalleryItemBackground);  //
                      }
                      else {
                            i = (ImageView) convertView;
                      }
                      return i;
                      
                }
                
                
              ////
                public TypedArray obtainStyledAttributes(int theme) {   
             	    // TODO Auto-generated method stub   
             	    return null;   
             	}   
                ////
    
                /** Returns the size (0.0f to 1.0f) of the views 
                 * depending on the 'offset' to the center. */ 
                public float getScale(boolean focused, int offset) { 
                  /* Formula: 1 / (2 ^ offset) */ 
                    return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
                }
                      
                      //////////////////
                     /* 
                      ImageView i = new ImageView(myContext);   
               	   
                      Bitmap bm = BitmapFactory.decodeFile(   
                        FileList.get(position).toString());   
                      i.setImageBitmap(bm);   
                     
                      i.setLayoutParams(new Gallery.LayoutParams(150, 100));   
                      i.setScaleType(ImageView.ScaleType.FIT_XY);   
                      i.setBackgroundResource(mGalleryItemBackground);   
                     
                      return i; */
                      ///////////////////
                }
          }
    quand je mets la curseur sur la ligne le message suivant est affiché:
    The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView<?>, View, int, long)

  4. #4
    Membre éclairé

    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    51
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 51
    Par défaut
    must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick

    Je croix que le problemme n'est qu'un typo - ton méthod commence par O majuscule: public void OnItemClick.


    Ta gallérie a l'air très bien réalisé mais je mentionne pour mémoire que tu as aussi l'option d'utiliser la gallérie native comme ca :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Intent intent =  new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("video/*");
    startActivityForResult(intent, 1);
    Et récupérer la ressource sélectionnée dans ton implémentation de onActivityResult.

    J'espère que tu le trouve utile.

  5. #5
    Membre confirmé
    Inscrit en
    Décembre 2008
    Messages
    233
    Détails du profil
    Informations personnelles :
    Âge : 38

    Informations forums :
    Inscription : Décembre 2008
    Messages : 233
    Par défaut
    merci pour la réponse je vais tester ça

Discussions similaires

  1. comment afficher une video d'une base de donnee
    Par simabd dans le forum Langage
    Réponses: 1
    Dernier message: 06/03/2015, 17h15
  2. Comment afficher une pub durant la lecture d'une video?
    Par abouilyas dans le forum Général Conception Web
    Réponses: 0
    Dernier message: 26/01/2015, 18h32
  3. [RpiCam] Comment afficher la date sur une image ou une video
    Par plawyx dans le forum Raspberry Pi
    Réponses: 2
    Dernier message: 25/01/2015, 21h37
  4. Comment afficher le titre de la video sur youtube.
    Par hacker59 dans le forum VB.NET
    Réponses: 0
    Dernier message: 14/04/2014, 20h02
  5. C# sous Ubuntu: comment afficher une video.
    Par Gamagnyax dans le forum C#
    Réponses: 1
    Dernier message: 02/03/2012, 01h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo