我正在使用OkHttp lib下载一些图片,一切正常,但所有这些都出现在最近的文件中。有没有办法禁用我的照片在那里。 我的代码是:
public static void downloadSimpleFile(String urlLink, String myPath, String fileName) {
OutputStream outputStream = new FileOutputStream(new File(myPath + fileName));
byte[] bytes = new byte[1024];
int read;
final OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(5, TimeUnit.SECONDS);
client.setReadTimeout(5, TimeUnit.SECONDS);
client.setWriteTimeout(5, TimeUnit.SECONDS);
Request request = new Request.Builder()
.url(urlLink)
.build();
try {
Response response = client.newCall(request).execute();
if (response != null) {
InputStream inputStream = response.body().byteStream();
if (inputStream != null) {
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
}
} catch (IOException e) {
//exception code treatement
}
}
修改
像评论中提到的@Knossos一样,我创建了这个.nomedia文件,但我的照片仍然出现,使用代码:public static void createNoMediaFile() {
File file = new File(mypath + ".nomedia");
if (!file.exists()) {
try {
if (file.createNewFile()) {
//is created
}
} catch (IOException e) {
//exception
}
}
}