java異步上傳圖片示例
更新時間:2014年02月10日 14:27:35 作者:
這篇文章主要介紹了java異步上傳圖片示例,需要的朋友可以參考下
復制代碼 代碼如下:
final File imageFile = new File(getCacheDir().getPath() + "/img/" + p.image);
image.setVisibility(View.GONE);
view.findViewById(R.id.imageLoading).setVisibility(View.VISIBLE);
(new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
try {
Bitmap image;
if (!imageFile.exists() || imageFile.length() == 0) {
image = BitmapFactory.decodeStream(new URL(
"http://example.com/images/"
+ p.image).openStream());
image.compress(Bitmap.CompressFormat.JPEG, 85,
new FileOutputStream(imageFile));
image.recycle();
}
image = BitmapFactory.decodeFile(imageFile.getPath(),
bitmapOptions);
return image;
} catch (MalformedURLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap image) {
if (view.getTag() != p) // The view was recycled.
return;
view.findViewById(R.id.imageLoading).setVisibility(
View.GONE);
view.findViewById(R.id.image)
.setVisibility(View.VISIBLE);
((ImageView) view.findViewById(R.id.image))
.setImageBitmap(image);
}
}).execute();
您可能感興趣的文章:
相關(guān)文章
Java反射之靜態(tài)加載和動態(tài)加載的簡單實例
下面小編就為大家?guī)硪黄狫ava反射之靜態(tài)加載和動態(tài)加載的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
Springboot動態(tài)切換數(shù)據(jù)源的具體實現(xiàn)與原理分析
目前有個需求,需要使用不同的數(shù)據(jù)源,例如某業(yè)務要用A數(shù)據(jù)源,另一個業(yè)務要用B數(shù)據(jù)源,所以下面這篇文章主要給大家介紹了關(guān)于Springboot動態(tài)切換數(shù)據(jù)源的具體實現(xiàn)與原理分析,需要的朋友可以參考下2021-12-12
Java數(shù)據(jù)結(jié)構(gòu)及算法實例:冒泡排序 Bubble Sort
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:冒泡排序 Bubble Sort,本文直接給出實現(xiàn)代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-06-06
基于HTML5+js+Java實現(xiàn)單文件文件上傳到服務器功能
應公司要求,在HTML5頁面上實現(xiàn)上傳文件到服務器功能,對于我這樣的菜鳥,真是把我難住了,最后還是請教大神搞定的,下面小編把例子分享到腳本之家平臺,供大家參考2017-08-08

