2016-11-22 26 views
-2

我是android新手。我需要下載一張圖片並在活動中顯示該圖片。我試着更多我不能得到輸出。 任何人都可以幫助我?使用服務下載圖片

public class Home_Appliances extends AppCompatActivity { 
private static final String TAG = Home_Appliances.class.getSimpleName(); 

// Movies json url 
private static final String url = "http://quickiz.com/abdullah/json/pickspage?commonId=125"; 

private ProgressDialog pDialog; 
private List<City> cityList = new ArrayList<City>(); 
private ListView listView; 
private CustomListAdapter adapter; 

private TextView textView; 
private BroadcastReceiver receiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle bundle = intent.getExtras(); 
     if (bundle != null) { 
      String string = bundle.getString(DownloadService.FILEPATH); 
      int resultCode = bundle.getInt(DownloadService.RESULT); 
      if (resultCode == RESULT_OK) { 
       Toast.makeText(Home_Appliances.this, 
         "Download complete. Download URI: " + string, 
         Toast.LENGTH_LONG).show(); 
       textView.setText("Download done"); 
      } else { 
       Toast.makeText(Home_Appliances.this, "Download failed", 
         Toast.LENGTH_LONG).show(); 
       textView.setText("Download failed"); 
      } 
     } 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home__appliances); 
    textView = (TextView) findViewById(R.id.status); 

    listView = (ListView) findViewById(R.id.list); 
    /*adapter = new CustomListAdapter(this, cityList); 
    listView.setAdapter(adapter);*/ 
    pDialog = new ProgressDialog(this); 

    pDialog.setMessage("Loading..."); 
    pDialog.show(); 
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
      url, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.d(TAG, response.toString()); 
        hidePDialog(); 
        JSONArray jsonarr = null; 
        try { 
         jsonarr = response.getJSONArray("productDetails"); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        for (int i = 0; i < jsonarr.length(); i++) { 


         JSONObject obj = null; 
         try { 
          obj = jsonarr.getJSONObject(i); 

          City city = new City(); 
          city.setTitle(obj.getString("productName")); 
          city.setThumbnailUrl(obj.getString("Image")); 

          // adding movie to movies array 
          cityList.add(city); 


         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 

        //adapter.notifyDataSetChanged(); 
       } 
      }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d(TAG, "Error: " + error.getMessage()); 
      pDialog.hide(); 
     } 
    }); 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(jsonObjReq); 

} 

public void onDestroy() { 
    super.onDestroy(); 
    hidePDialog(); 
} 

private void hidePDialog() { 
    if (pDialog != null) { 
     pDialog.dismiss(); 
     pDialog = null; 
    } 
} 

protected void onResume() { 
    super.onResume(); 
    registerReceiver(receiver, new IntentFilter(
      DownloadService.NOTIFICATION)); 
} 
@Override 
protected void onPause() { 
    super.onPause(); 
    unregisterReceiver(receiver); 
} 

public void onClick(View view) { 



    Intent intent = new Intent(this, DownloadService.class); 
    // add infos for the service which file to download and where to store 
    intent.putExtra(DownloadService.FILENAME, "MyImages"); 
    intent.putExtra(DownloadService.URL,cityList.get(0).getThumbnailUrl()); 
    startService(intent); 
    textView.setText("Service started"); 
    System.out.println("-----URL image---------"+cityList.get(0).getThumbnailUrl()); 
} 
} 

這是我的服務類

public class DownloadService extends IntentService { 

private int result = Activity.RESULT_CANCELED; 
public static final String URL = "urlpath"; 
public static final String FILENAME = "filename"; 
public static final String FILEPATH = "filepath"; 
public static final String RESULT = "result"; 
public static final String NOTIFICATION = "com.vogella.android.service.receiver"; 

public DownloadService() { 
    super("DownloadService"); 
} 

// will be called asynchronously by Android 
@Override 
protected void onHandleIntent(Intent intent) { 
    String urlPath = intent.getStringExtra(URL); 
    System.out.println("urlpath=================================>"+urlPath); 
    String fileName = intent.getStringExtra(FILENAME); 
    File output = new File(Environment.getExternalStorageDirectory(), 
      fileName); 
    if (output.exists()) { 
     output.delete(); 
     System.out.println("am in download if class=============================>"); 
    } 

    InputStream stream = null; 
    FileOutputStream fos = null; 
    try { 

     URL url = new URL(urlPath); 
     stream = url.openConnection().getInputStream(); 
     InputStreamReader reader = new InputStreamReader(stream); 
     fos = new FileOutputStream(output.getPath()); 
     int next = -1; 
     while ((next = reader.read()) != -1) { 
      fos.write(next); 
     } 
     // successfully finished 
     result = Activity.RESULT_OK; 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (stream != null) { 
      try { 
       stream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if (fos != null) { 
      try { 
       fos.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    publishResults(output.getAbsolutePath(), result); 
} 

private void publishResults(String outputPath, int result) { 
    Intent intent = new Intent(NOTIFICATION); 
    intent.putExtra(FILEPATH, outputPath); 
    intent.putExtra(RESULT, result); 
    sendBroadcast(intent); 
} 
} 

回答

0

創建映像以保存圖像,然後使用畢加索下載和在ImageView的插入圖像。查找廣場上的畢加索圖書館。

在您的搖籃添加一行:

compile 'com.squareup.picasso:picasso:2.5.2' 

然後在您的活動:Picasso.with(context).load("YourURL").into(imageView);

ImageView的是你的ImageView你想讓它顯示在

+0

u能告訴我怎麼用。畢加索在這裏 – buvi