qq_38004389 2017-10-28 08:48 采纳率: 100%
浏览 1334
已采纳

Android断点续传无法实例化RandomAccessFile

代码如下:package com.download.services;

import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import com.download.entities.FileInfo;

import java.io.File;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**

  • Created by Administrator on 2017/10/27.
    */
    public class DownloadService extends android.app.Service {

    public static final String DOWNLOAD_PATH =
    Environment.getDownloadCacheDirectory().getAbsolutePath() +
    "/downloads/";
    public static final String ACTION_START = "ACTION_START";
    public static final String ACTION_STOP = "ACTION_STOP";
    public static final int MSG_INIT = 0;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    if (ACTION_START.equals(intent.getAction())) {
    FileInfo fileInfo = (FileInfo) intent.getSerializableExtra("fileInfo");
    Log.i("test", "Start:" + fileInfo);
    //启动初始化线程
    new InitThread(fileInfo).start();
    } else if (ACTION_STOP.equals(intent.getAction())) {
    FileInfo fileInfo = (FileInfo) intent.getSerializableExtra("fileInfo");
    Log.i("test", "Stop:" + fileInfo.toString());
    }
    return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
    return null;
    }

    Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case MSG_INIT:
    FileInfo fileInfo = (FileInfo) msg.obj;
    Log.i("test", "Init:" + fileInfo);
    break;
    }
    super.handleMessage(msg);
    }
    };

    /**

    • 初始化子线程
      */
      class InitThread extends Thread {
      private FileInfo mFileInfo = null;

      public InitThread(FileInfo mFileInfo) {
      this.mFileInfo = mFileInfo;
      }

      public void run() {
      HttpURLConnection con = null;

      try {
          //连接网络文件
          URL url = new URL(mFileInfo.getUrl());
          con = (HttpURLConnection) url.openConnection();
          con.setConnectTimeout(3000);
          con.setRequestMethod("GET");
          int length = -1;
          if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
              //获得文件长度
              length = con.getContentLength();
          }
          if (length <= 0) {
              return;
          }
          File dir = new File(DOWNLOAD_PATH);
          //文件路径是否存在
          if (!dir.exists()) {
              //创建路径
              dir.mkdir();
          }
      
          //在本地创建文件
          File file = new File(dir, mFileInfo.getFileName());
          file.createNewFile();
          RandomAccessFile raf = new RandomAccessFile(file, "r");
          //设置文件长度
          raf.setLength(length);
          mFileInfo.setLength(length);
          mHandler.obtainMessage(MSG_INIT, mFileInfo).sendToTarget();
          try {
              con.disconnect();
      

      // raf.close();
      } catch (Exception e) {
      e.printStackTrace();
      }
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      }
      }
      我是初学Android,会不会是Android虚拟机不支持外置内存卡的问题?而且使用raf.close时会报raf为初始化

  • 写回答

1条回答 默认 最新

  • qq_38004389 2017-10-28 09:19
    关注

    已解决
    getDownloadCacheDirectory():下载缓存内容目录。
    getExternalStorageDirectory():主要的外部存储目录。一些不支持外部存储的手机此方法获得的是内置存储的目录

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU