我是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 :(得分:0)
创建一个图像视图来保存图像,然后使用Picasso下载并在imageview中插入图像。查看毕加索图书馆的广场。
在Gradle中添加以下行:
compile 'com.squareup.picasso:picasso:2.5.2'
然后在您的活动中:Picasso.with(context).load("YourURL").into(imageView);
imageView是您希望它显示在您的imageview。