我下载从互联网上的图像,这是我选择它保存在手机上的路径recyclerview图片:问题用毕加索
ContextWrapper cw = new ContextWrapper(mContext);
File directory = cw.getDir("imagesDB", Context.MODE_PRIVATE);
OutputStream output = new FileOutputStream(new File(directory,"profile.jpg"));
图像已被下载我用这个来后获取路径:
String databasePath = mContext.getDir("", Context.MODE_PRIVATE).getAbsolutePath();
databasePath = databasePath + "imagesDB/profile.jpg";
然后在我的recyclerview适配器我用这个:
Picasso.with(context).load(databasePath).placeholder(R.mipmap.ic_launcher).into(holder.Photo);
我总是得到显示ic_launcher图像而不是我下载的图像。
我是否使用错误的路径的图像?
这是我用来下载图像的代码:
URL url = null;
try {
url = new URL("http://192.168.0.100/app/image/image1.jpg");
} catch (MalformedURLException e) {
e.printStackTrace();
}
InputStream input = null;
try {
input = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
ContextWrapper cw = new ContextWrapper(mContext);
File directory = cw.getDir("imagesDB", Context.MODE_PRIVATE);
OutputStream output = new FileOutputStream(new File(directory,"profile.jpg"));
try {
byte[] buffer = new byte[10000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
String databasePath = mContext.getDir("", Context.MODE_PRIVATE).getAbsolutePath();
Log.i("","Path1: "+ databasePath.toString());
databasePath = databasePath + "imagesDB/profile.jpg";
Log.i("","Path2: "+ databasePath.toString());
} finally {
output.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
首先在Log中打印_databasePath_并检查其值。 – Piyush
我认为图像需要从服务器下载并保存时间。并且在下载之前使用picaso附加图像。 –
你有没有给你的清单写读权限?同时检查你的变量是否有合适的路径 – GrIsHu