java.file.io没有得到解决

时间:2017-07-20 07:15:46

标签: java android gridview file-io video-gallery

我一直在努力为自己制作一个视频库并被困在这里。按照此链接获取一些参考资料,但仍然遇到一些问题:

For making thumbnails for the videos

这是我的代码:

public class AddFragment extends Fragment {

private ImageButton imageButton;
private GridView gridView;

private File files;

ArrayList<File> list;

public AddFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add, container, false);
    imageButton = (ImageButton) view.findViewById(R.id.gotoButton);
    gridView = (GridView) view.findViewById(R.id.grid_view);
    gridView.setAdapter(new ImageAdapter(getContext()));
    list = videoReader(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES));


    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            //for making the button visible as ssonas the item gets selected
            imageButton.setVisibility(view.VISIBLE);

        }
    });

    return view;
}

ArrayList<File> videoReader(File root) {

    ArrayList<File> arrayList = new ArrayList<>();

    File[] file = root.listFiles();

    for(int i=0;i<file.length;i++){
        if(file[i].isDirectory()){

        }else{
            if(file[i].getName().endsWith(".mp4")){
                arrayList.add(file[i]);
            }
        }
    }

   return arrayList;
}


public class ImageAdapter extends BaseAdapter{

    private Bitmap bitmap;

    private final Context context;

    private ImageAdapter(Context c){
            context = c;
    }

    //for the video numbers
    @Override
    public int getCount() {
        return list.size();
    }

   //for getting the video items position vise
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            if(list.get(position).contains(".jpg"))
            {
                bitmap = BitmapFactory.decodeFile(list.get(position)); //Creation of Thumbnail of image
            }
            else if(list.get(position).contains(".mp4"))
            {
                bitmap = ThumbnailUtils.createVideoThumbnail(list.get(position), 0); //Creation of Thumbnail of video
            }
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(8, 8, 8, 8);
            picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else
        {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}  }

我遇到的问题是在getView方法的 ImageAdapter类中

这些是:

1。在if(list.get(position).contains(&#34; .jpg&#34;))//无法解析包含

2。在bitmap = BitmapFactory.decodeFile(list.get(position)); //说来自Bitmapfacotory的decodeFile(java.lang.string)无法应用于(java.file.io)

P.S。对于第二个选项,我在尝试从此链接获取后尝试这样做但失败了:

Java contradicting behavior resolved

请帮助我感激不尽。我做了很多研究,我在这里遵循了Stackoverflow的所有规则。

2 个答案:

答案 0 :(得分:1)

试试这个。

if(list.get(position).contains(".jpg"))
{
     bitmap = BitmapFactory.decodeFile(list.get(position).toString()); 
}

list.get(position)是File对象,你需要传递String对象,所以只需通过写.toString()使它成为String。

答案 1 :(得分:1)

  1. 包含期望看到与列表项相同的对象类型。如果你想检查文件是否是图像,请阅读:Android: How to check if file is image?
  2. 解码文件需要获取文件URL而不是文件对象。请参阅此处如何使用它:Bitmapfactory example