![](/img/trans.png)
[英]how to store downloaded image in internal storage using Download Manager in android
[英]how to download image from server and store in internal storage using JSON in android
我想使用json從服務器下載圖像並將其存儲在內部存儲中,然后逐個顯示圖像。 我有JSON格式,任何機構都可以幫我解決該問題嗎??? 我的json是:
{
"_id" : ObjectId("55b09029e56e5ecc1577f00e"),
"user" : ObjectId("559a298d9969172f3ffeaa6d"),
"name" : "ddd",
"language" : "english",
"pages" :
[ {
"_id" : ObjectId("55b0902be56e5ecc1577f00f"),
"page_image" : [
{ "image" : "images.png", "_id" : ObjectId("55b09032e56e5ecc1577f010") },
{ "image" : "20140624_172041_fbhciha_sm.jpeg", "_id" : ObjectId("55b0903ce56e5ecc1577f011") },
{ "image" : "673.jpg", "_id" : ObjectId("55b09042e56e5ecc1577f012") } ] } ], "__v" : 4 }
最簡單的方法之一是使用Glide庫( https://github.com/bumptech/glide ),您可以將位圖獲取為
Bitmap bitmap = Glide.with(pContext).load(url).asBitmap().into(100, 100).get();
然后保存位圖
確保在新線程中調用該方法
您得到String作為對服務器的響應。 然后創建json對象
String strResponse = ...
JSONObject jsonObject = new JSONObject(strResponse);
JSONArray pagesArray = jsonObject.getJSONArray("pages");
for (int i=0;i<pagesArray.length();i++){
JSONObject pageObj = pagesArray.getJSONObject(i);
JSONArray pageImagesArray = pageObj.getJSONArray("page_image");
for (int j=0;j<pageImagesArray.length();j++) {
JSONObject image = pageImagesArray.getJSONObject(j);
String urlImg = image.getString("image");
}
}
1-在build.gradle內添加compile 'com.squareup.picasso:picasso:2.5.2'
或從Picasso下載jar文件
2- Picasso.with(this) .load("Your image url here") .into(target);
如果您列出,則還可以在遍歷列表時在任何循環內使用以上內容。
3-作為上一類中的目標,顯示編譯時錯誤。 將以下代碼作為您的內部類。
private Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/image1.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 80, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}}
}).start(); }
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
if (placeHolderDrawable != null) {} }};
4-如果您在理解上述代碼時仍然遇到問題,請參考以下示例。 Picasso-Android的圖像下載和緩存庫
5-`for(int i = 0; i <cons.length(); i ++)
{
JSONObject c = cons.getJSONObject(i);
imgURL = c.getString(image);
Picasso.with(this)
.load(imgURL)
.into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/image1.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.