在我的应用中,我正在使用this template并下载我使用ImageDownloader的图片,这在其他应用上运行良好。当我回到图片所在的标签/片段时,我可以看到下载对话框,它会消失。
logcat的:
11-13 20:23:11.856: I/Async-Example(775): onPreExecute Called
11-13 20:23:11.956: D/dalvikvm(775): GC_CONCURRENT freed 80K, 2% free 11133K/11271K, paused 28ms+31ms, total 130ms
11-13 20:23:12.126: D/libEGL(775): loaded /system/lib/egl/libEGL_emulation.so
11-13 20:23:12.146: D/(775): HostConnection::get() New Host Connection established 0x2a0f48e8, tid 775
11-13 20:23:12.156: D/libEGL(775): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-13 20:23:12.165: D/libEGL(775): loaded /system/lib/egl/libGLESv2_emulation.so
11-13 20:23:12.186: E/ImageDownloader(775): Something went wrong while retrieving bitmap.
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lupradoa.lakari"
android:versionCode="1"
android:versionName="1.0">
<permission
android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme"
android:allowBackup ="false">
<activity android:name="com.lupradoa.lakari.base.AppMainTabActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我认为这里有罪的是“ onPreExecute ()”
FragmentA.java:
package com.lupradoa.lakari.fragmenttabstudy.tabA;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import com.lupradoa.lakari.R;
import com.lupradoa.lakari.base.AppConstants;
import com.lupradoa.lakari.base.BaseFragment;
public class AppTabAFirstFragment extends BaseFragment {
private Button mGotoButton;
private ImageView downloadedImg;
private ProgressDialog simpleWaitDialog;
private String downloadUrl = "http://www.9ori.com/store/media/images/8ab579a656.jpg";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_tab_a_first_screen, container, false);
mGotoButton = (Button) view.findViewById(R.id.id_next_tab_a_button);
mGotoButton.setOnClickListener(listener);
//image downloaded
downloadedImg =(ImageView) view.findViewById(R.id.imageView);
new ImageDownloader().execute(downloadUrl);
return view;
}
private OnClickListener listener = new View.OnClickListener(){
@Override
public void onClick(View v){
/* Go to next fragment in navigation stack*/
mActivity.pushFragments(AppConstants.TAB_A, new AppTabASecondFragment(),true,true);
}
};
private class ImageDownloader extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground (String... param){
return downloadBitmap(param[0]);
}
@Override
protected void onPreExecute(){
Log.i("Async-Example", "onPreExecute Called");
**//NOT SURE IF THE CONTEXT IS SET PROPERLY**
simpleWaitDialog = ProgressDialog.show(getActivity(), "Wait", "Downloading Image");
}
@Override
protected void onPostExecute(Bitmap result) {
Log.i("Async-Example", "onPostExecute Called");
downloadedImg.setImageBitmap(result);
simpleWaitDialog.dismiss();
}
private Bitmap downloadBitmap(String url){
final DefaultHttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
try{
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK){
Log.w("ImageDownloader", "Error" + statusCode + "while retrieving bitmap from "+ url);
return null;
}
final HttpEntity entity = response.getEntity();
if(entity != null){
InputStream inputStream = null;
try{
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}finally {
if(inputStream !=null){
inputStream.close();
}
entity.consumeContent();
}
}
}catch (Exception e){
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while retrieving bitmap.");
}
return null;
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:background="#aa5500"
android:gravity="center">
<ImageView
android:id="@+id/imageView"
android:layout_width="320dp"
android:layout_height="0dp"
android:layout_weight="1"
android:contentDescription="@string/description"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/id_next_tab_a_button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="@string/como_llegar" />
</LinearLayout>
如何解决这个问题?
答案 0 :(得分:0)
我不认为问题出现在onPreExecute()上,但可能下载的图片太大而无法管理。
我建议您使用UniversalImageLoader来加载图片,它是许多应用中使用的功能齐全的库,可以正常工作。也许它也可以帮到你。