qq_38004389
2017-10-28 08:48
采纳率: 100%
浏览 1.3k

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():主要的外部存储目录。一些不支持外部存储的手机此方法获得的是内置存储的目录

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题