Android java.io.FileNotFoundException:/storage/emulated/0/Folder/Images/Folder/210.JPG.JPG(没有这样的文件或目录)

Android java.io.FileNotFoundException:/storage/emulated/0/Folder/Images/Folder/210.JPG.JPG(没有这样的文件或目录),android,file,directory,Android,File,Directory,使用Glide Library从服务器下载图像并在Gridview中显示,同时下载图像并保存在设备文件夹中,但未创建文件夹 它是GiveFileNotFoundException。当项目调试时,调试会显示完整的目录路径和图像,但并没有创建目录。我已授予在清单文件中写入外部存储的权限 public class GridViewAdapter extends ArrayAdapter { public Context context; public int resource;

使用Glide Library从服务器下载图像并在Gridview中显示,同时下载图像并保存在设备文件夹中,但未创建文件夹

它是Give
FileNotFoundException
。当项目调试时,调试会显示完整的目录路径和图像,但并没有创建目录。我已授予在清单文件中写入外部存储的权限

public class GridViewAdapter extends ArrayAdapter {

    public Context context;
    public int resource;
    public ArrayList data = new ArrayList();

    int downloadedSize = 0, totalsize;

    float per = 0;

  public GridViewAdapter(Context context, int resource, ArrayList data) {
        super(context, resource, data);

        this.context = context;
        this.resource = resource;
        this.data = data;
    }

    public void setGridData(ArrayList data){
        this.data = data;
        notifyDataSetChanged();
    }


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


        View row = convertView;
        ViewHolder holder = null;

        TextView tv_loading;





        long refID;


        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(resource, parent, false);
            holder = new ViewHolder();
           //  holder.tv_loading = (TextView) row.findViewById(R.id.text);
            holder.image = (ImageView) row.findViewById(R.id.image);
           row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        final ImageItem item = (ImageItem) data.get(position);


        Glide.with(context)
                .load(item.getImage())
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).override(200, 200))


                .into(holder.image);

        downloadFile(item.getImage(),item.getTitle(),item.getPathname());

        /*Glide.with(context)
                .asBitmap()
                .load(item.getImage())

                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        saveImage(resource,item.getPathname(),item.getImage());
                    }
                });*/


        return row;
    }


    static class ViewHolder {
        TextView tv_loading;
        ImageView image;
    }


    File downloadFile(String imageUrl, String Title,String PathName) {
        File file = null;
        try {





            URL url = new URL(imageUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url
                    .openConnection();

            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(false);

            // connect
            urlConnection.connect();

            // set the path where we want to save the file
            File SDCardRoot = Environment.getExternalStorageDirectory();

            File dir = new File(SDCardRoot.getAbsolutePath() + "/Sankalp Pre School/Images/"+ Title);
            if (!dir.exists()) {
                dir.mkdirs();

            }

            Date d = new Date();
            CharSequence s = DateFormat
                    .format("yyyyMMdd", d.getTime());
            Random ran = new Random();
            int n = 10000;
            n = ran.nextInt(n);




            file = new File(dir,PathName +".jpg");




            FileOutputStream fileOutput = new FileOutputStream(file);

            // Stream used for reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();

            // this is the total size of the file which we are
            // downloading
            totalsize = urlConnection.getContentLength();
//            setText("Starting PDF download...");

            // create a buffer...
            byte[] buffer = new byte[1024 * 1024];
            int bufferLength = 0;

            while ((bufferLength = inputStream.read(buffer)) > 0) {
                fileOutput.write(buffer, 0, bufferLength);
                downloadedSize += bufferLength;
                per = ((float) downloadedSize / totalsize) * 100;
  /*              tv_loading.setText("Total PDF File size  : "
                        + (totalsize / 1024)
                        + " KB\n\nDownloading PDF " + (int) per
                        + "% complete");*/
            }
            // close the output stream when complete //
            fileOutput.close();
            //tv_loading.setText("Download Complete. Open PDF Application installed in the device.");
            Toast.makeText(context, "Download Complete.", Toast.LENGTH_SHORT).show();

        } catch (final MalformedURLException e) {
            //setTextError("Some error occured. Press back and try again.",
            e.printStackTrace();
            Toast.makeText(context, "Some error occured. Press back and try again.", Toast.LENGTH_SHORT).show();
              //      Color.RED);
        } catch (final IOException e) {
            e.printStackTrace();
            //setTextError("Some error occured. Press back and try again.",
            Toast.makeText(context, "Some error occured. Press back and try again.", Toast.LENGTH_SHORT).show();
                //    Color.RED);
        } catch (final Exception e) {
            e.printStackTrace();
            //setTextError(
                    //"Failed to download image. Please check your internet connection.",
                  //  Color.RED);
            Toast.makeText(context, "Failed to download image. Please check your internet connection.", Toast.LENGTH_SHORT).show();
        }
        return file;
    }
公共类GridViewAdapter扩展了ArrayAdapter{
公共语境;
公共资源;
public ArrayList data=new ArrayList();
int downloadedSize=0,totalsize;
浮点数=0;
公共GridViewAdapter(上下文、int资源、ArrayList数据){
超级(上下文、资源、数据);
this.context=上下文;
这个资源=资源;
这个数据=数据;
}
公共void setGridData(ArrayList数据){
这个数据=数据;
notifyDataSetChanged();
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
ViewHolder=null;
文本视图电视加载;
长时间刷新;
if(行==null){
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(资源,父项,false);
holder=新的ViewHolder();
//holder.tv_load=(TextView)row.findViewById(R.id.text);
holder.image=(ImageView)row.findViewById(R.id.image);
row.setTag(支架);
}否则{
holder=(ViewHolder)row.getTag();
}
最终ImageItem=(ImageItem)数据。获取(位置);
带(上下文)滑动
.load(item.getImage())
.apply(new RequestOptions().diskCacheStrategy(diskCacheStrategy.ALL).override(200200))
.插入(支架图像);
下载文件(item.getImage(),item.getTitle(),item.getPathname());
/*带(上下文)滑动
.asBitmap()
.load(item.getImage())
.into(新的SimpleTarget(){
@凌驾

public void onResourceReady(@NonNull Bitmap resource,@Nullable transition它是在SD卡上还是内部存储的