java.file.io 没有得到解决

java.file.io is not getting resolved out

我一直在努力为自己制作一个视频库,但卡在了这里。按照此 link 进行了一些参考,但仍然遇到一些问题:

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;
    }
}  }

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

这些是:

1.在 if(list.get(position).contains(".jpg")) //无法解析 contains

2。在位图中 = BitmapFactory.decodeFile(list.get(位置)); // 表示 Bitmapfacotory 中的 decodeFile(java.lang.string) 不能应用于 (java.file.io)

P.S。对于第二个选项,我在从 link 获得参考后尝试这样做但失败了:

Java contradicting behavior resolved

试试这个。

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

list.get(position) 是 File 对象,您需要传递 String 对象,因此只需编写 .toString() 使其成为 String。

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