我下載從互聯網上的圖像,這是我選擇它保存在手機上的路徑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