此博客长期更新,用于记录在开发过程中遇到的各种坑,欢迎各路大神在评论区添加内容,指正错误!
- 报错信息:
Program type already present: android.support.v13.view.DragAndDropPermissionsCompat
;
出错原因:第三方库或者module之间V13支持库冲突了;
解决方法:在build.gradle中的dependencies节点下添加如下configurations内容
dependencies {
configurations {
all*.exclude group: 'com.android.support', module: 'support-v13'
}
compile 'com.android.support:appcompat-v7:28.0.0-beta01'
testCompile 'junit:junit:4.12'
}
- 出错场景:在使用Android Studio 中添加openCV依赖时出现此问题
报错信息:Unable to resolve dependency for ‘:app@debug/compileClasspath’: Could not resolve project :java.
出错截图:
出错原因:不是很确定,但应该是与Gradle版本有关,依赖的module的Gradle版本过低,但是编译的Gradle版本过高;
解决方案:这个问题针对不同情况解决办法也不一样,具体大家可以看一下这几篇博客,这里我的情况是openCV下载版本的问题,换了个版本重新导入编译构建就可以了,大家在添加第三方依赖时,如果出现此问题也可以考虑更换一个版本重新尝试;
- 出错场景:java多线程下载一张网络上图片时,每次下载数据总会丢失一部分,图片像素不完整,经过多次测试,发现开启的三个下载线程每次只能随机启动一个,故文件每次只能下载1/3,。
报错信息:无报错信息
原因分析:尚在定位中。。。(解决后会进行更新)
完整代码如下:
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class downLoad {
//定义文件路径
static String path = new String("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577292096535&di=f9dd01657df0d177384a9ab6fb221afb&imgtype=0&src=http%3A%2F%2Fpic.jj20.com%2Fup%2Fallimg%2F711%2F0ZG3120214%2F130ZG20214-13.jpg");
//暂定为分三个线程下载数据
private static final int threadCounter = 3;
private static File dst;
public static void main(String[] args) {
int startIndex,endIndex;
//1.先拿到文件大小,用来计算每个线程下载的开始和结束位置
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if(code == 200) {
//范围值为文件大小,单位是字节
int length = conn.getContentLength();
System.out.println(length);
//2.创建一个同待下载文件同样大小的文件,这里考虑到要进行随机存储
dst = new File("test.jpg");
//计算每个线程需要下载的字节数,设置开始和结束位置
int blockSize = length / threadCounter;
for(int i = 0;i<threadCounter;i++) {
startIndex = i * blockSize;
endIndex = (i+1) * blockSize-1;
if (i == threadCounter-1) {
endIndex = length -1;
}
//开启线程进行下载
DownLoadThread thread = new DownLoadThread(startIndex,endIndex);
thread.setName("线程" + (i + 1));
thread.start();
}
}
}catch (Exception e) {
// TODO: handle exception
}
}
private static class DownLoadThread extends Thread{
private int startIndex;
private int endIndex;
//通过构造方法传入下载开始和结束位置
public DownLoadThread(int startIndex,int endIndex) {
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
super.run();
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
//设置一个请求头Range,作用是高速服务器下载的开始和结束位置
conn.setRequestProperty("range", "bytes="+startIndex+"-"+endIndex);
int code = conn.getResponseCode();
//状态码206表示请求部分资源成功
if(code == 206) {
RandomAccessFile randomAccessFile = new RandomAccessFile(dst, "rw");
randomAccessFile.seek(startIndex);
InputStream inputStream = conn.getInputStream();
//将流写入到文件中
int len = -1;
byte[]buffer = new byte[1024];
while((len=inputStream.read(buffer))!=-1) {
randomAccessFile.write(buffer,0,len);
}
randomAccessFile.close();
}
System.out.println(Thread.currentThread().getName()+"下载完毕");
}catch (Exception e) {
// TODO: handle exception
}
}
}
}