视频未在 GridView 中呈现

发布于 2024-09-10 00:19:55 字数 4413 浏览 3 评论 0原文

在解决了我之前的所有问题以运行代码以从 SD 卡检索视频并将其显示在 GridView 中之后,我仍然看不到视频。我不知道我做错了什么,他们不会显示并且数组没有传递。

如果有人可以帮助我,请,因为我真的不知道出了什么问题... :(( 谢谢...

这是尝试在 GridView 中检索和显示 sdcard 中的视频的 java 文件:

package com.mobilevideoeditor.moved;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
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.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.VideoView;




public class EditGalleryView extends Activity {
    Uri[] vidUris;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videogrid);

        GridView vGrid=(GridView) findViewById(R.id.vgrid);
        vGrid.setAdapter(new VideoAdapter(this));

        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

        Log.d("EditGalleryView", "uri:"+uri);
        String[] projection = {
                MediaStore.Video.Media.DESCRIPTION,
                MediaStore.Video.Media.DATA  
    };

        Cursor c = this.managedQuery(uri, projection, null, null,
                MediaStore.Video.Media.DATE_ADDED);
                         Log.d("EditGalleryView", "vids available:" +c.getCount());

                         ArrayList<Uri> experimentVids = new ArrayList<Uri>();


                             if (c.getCount() != 0) {
                                 c.moveToFirst();
                                 experimentVids.add(Uri.parse(c.getString(1)));
                                 while (c.moveToNext()) {
                                         experimentVids.add(Uri.parse(c.getString(1)));

                                  }
                      }
                             Log.d("ClassName", "experimentVids.length:" +experimentVids.size());
                                                  if (experimentVids.size() != 0) {
                                                    vidUris = new Uri[experimentVids.size()];
                                                      for (int i = 0; i < experimentVids.size(); i++) {
                                                          vidUris[i] = experimentVids.get(i);
                                                      }
                                                      Log.d("EditGalleryView", "vidUris:"+vidUris.length);
                                                  }
                                              }


    public class VideoAdapter extends BaseAdapter {
        private Context mContext;

        public VideoAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            //return mThumbIds.length;
           if(vidUris!=null){
            return vidUris.length;}
        return 0;
        }


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

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

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
           VideoView videoView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                videoView = new VideoView(mContext);
                videoView.setVideoURI(vidUris[position]);
                videoView.setLayoutParams(new GridView.LayoutParams(85, 85));
               //videoView.setScaleType(VideoView.ScaleType.CENTER_CROP);
                videoView.setPadding(8, 8, 8, 8);
            } else {
                videoView = (VideoView) convertView;
            }

          //  imageView.setImageResource(mThumbIds[position]);
            return videoView;
        }

       /* // references to our images
        private Integer[] mThumbIds = {
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_2,
                R.drawable.sample_6, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_1,

        };*/

    }   

}

强制关闭错误 - 问题已解决


应用程序消失 - 问题已解决


阵列问题 - 问题已解决

After having solved every of my previous issues to get the code running for retrieving videos from the sdcard and displaying them in the GridView, I still cannot see the videos. I don't know what I have done wrong that they won't show and the array isn't passed on.

If someone could help me out, please, because I really haven't got a clue what's wrong... :(( Thank you...

This is the java file that tries to retrieve and display the videos from the sdcard in the GridView:

package com.mobilevideoeditor.moved;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
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.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.VideoView;




public class EditGalleryView extends Activity {
    Uri[] vidUris;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videogrid);

        GridView vGrid=(GridView) findViewById(R.id.vgrid);
        vGrid.setAdapter(new VideoAdapter(this));

        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

        Log.d("EditGalleryView", "uri:"+uri);
        String[] projection = {
                MediaStore.Video.Media.DESCRIPTION,
                MediaStore.Video.Media.DATA  
    };

        Cursor c = this.managedQuery(uri, projection, null, null,
                MediaStore.Video.Media.DATE_ADDED);
                         Log.d("EditGalleryView", "vids available:" +c.getCount());

                         ArrayList<Uri> experimentVids = new ArrayList<Uri>();


                             if (c.getCount() != 0) {
                                 c.moveToFirst();
                                 experimentVids.add(Uri.parse(c.getString(1)));
                                 while (c.moveToNext()) {
                                         experimentVids.add(Uri.parse(c.getString(1)));

                                  }
                      }
                             Log.d("ClassName", "experimentVids.length:" +experimentVids.size());
                                                  if (experimentVids.size() != 0) {
                                                    vidUris = new Uri[experimentVids.size()];
                                                      for (int i = 0; i < experimentVids.size(); i++) {
                                                          vidUris[i] = experimentVids.get(i);
                                                      }
                                                      Log.d("EditGalleryView", "vidUris:"+vidUris.length);
                                                  }
                                              }


    public class VideoAdapter extends BaseAdapter {
        private Context mContext;

        public VideoAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            //return mThumbIds.length;
           if(vidUris!=null){
            return vidUris.length;}
        return 0;
        }


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

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

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
           VideoView videoView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                videoView = new VideoView(mContext);
                videoView.setVideoURI(vidUris[position]);
                videoView.setLayoutParams(new GridView.LayoutParams(85, 85));
               //videoView.setScaleType(VideoView.ScaleType.CENTER_CROP);
                videoView.setPadding(8, 8, 8, 8);
            } else {
                videoView = (VideoView) convertView;
            }

          //  imageView.setImageResource(mThumbIds[position]);
            return videoView;
        }

       /* // references to our images
        private Integer[] mThumbIds = {
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_2,
                R.drawable.sample_6, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_1,

        };*/

    }   

}

Force close Error - ISSUE SOLVED


App Disappearance - ISSUE SOLVED


Array Issue - ISSUE SOLVED

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2024-09-17 00:19:55

使 vidUris 成为 EditGalleryView 类的成员。另外,我认为 ArrayList 上有一个 toArray 方法,它将返回一个元素数组。

Make vidUris a member of the EditGalleryView class. Also I think there is a toArray method on ArrayList that will return an array of elements.

痴意少年 2024-09-17 00:19:55

我也有同样的问题,解决方法也很简单,只需修改函数 public View getView(intposition, ViewconvertView,ViewGroupparent) 如下:

    public View getView(int position, View convertView, ViewGroup parent) {
        VideoView videoView;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            videoView = new VideoView(mContext);
            videoView.setFocusable(true);
            videoView.setVideoURI(vidUris[position]);
            videoView.setLayoutParams(new GridView.LayoutParams(160, 120));
            // videoView.setScaleType(VideoView.ScaleType.CENTER_CROP);
            videoView.setPadding(8, 8, 8, 8);
            videoView.start();
        } else {
            videoView = (VideoView) convertView;
            videoView.start();
        }

        // imageView.setImageResource(mThumbIds[position]);
        return videoView;
    }

现在你可以用上面的代码替换该函数并运行看看是否好的,它在我的已 root 的设备华为 c8815​​ 上运行正常

I have got the same question,and the resolution is also simple,just modify the function public View getView(int position, View convertView, ViewGroup parent) like below:

    public View getView(int position, View convertView, ViewGroup parent) {
        VideoView videoView;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            videoView = new VideoView(mContext);
            videoView.setFocusable(true);
            videoView.setVideoURI(vidUris[position]);
            videoView.setLayoutParams(new GridView.LayoutParams(160, 120));
            // videoView.setScaleType(VideoView.ScaleType.CENTER_CROP);
            videoView.setPadding(8, 8, 8, 8);
            videoView.start();
        } else {
            videoView = (VideoView) convertView;
            videoView.start();
        }

        // imageView.setImageResource(mThumbIds[position]);
        return videoView;
    }

and now you can replace the function with the above code and run to see if ok, it runs ok on my device huawei c8815 which is rooted

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文