gpt4 book ai didi

android - Imageloader 没有在真实设备上加载图像

转载 作者:搜寻专家 更新时间:2023-11-01 08:56:50 26 4
gpt4 key购买 nike

我正在使用 ImageLoader 类从 url 加载图像并将其显示在列表显示。但是这个 ImageLoader 正在模拟器上加载图像,当我在没有加载任何图像的真实设备上运行我的应用程序。(只是显示默认图片)。

请告诉我必须如何处理 ImageLoader 类才能使其在真实设备上运行。

ImageLoader Class:

public class ImageLoader {

MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
// Handler to display images in UI thread
Handler handler = new Handler();

public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}

final int stub_id = R.drawable.products;

public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}

private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}

private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);

Bitmap b = decodeFile(f);
if (b != null)
return b;

// Download Images from the Internet
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}

// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, o);
stream1.close();

// Find the correct scale value. It should be the power of 2.
// Recommended Size 512
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
FileInputStream stream2 = new FileInputStream(f);
Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
return bitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;

public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}

class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;

PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}

@Override
public void run() {
try {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
handler.post(bd);
} catch (Throwable th) {
th.printStackTrace();
}
}
}

boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}

// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;

public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}

public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}

public void clearCache() {
memoryCache.clear();
fileCache.clear();
}

}

My GalleryTab Class:--

public class GalleryTab extends Fragment {
GridView gridview;
ProgressDialog mProgressDialog;
GridViewAdapter adapter;
public List<GalleryList> phonearraylist = null;
View view;
private WeakReference<RemoteDataTask> asyncTaskWeakRef;

public static Fragment newInstance(Context context) {
GalleryTab f = new GalleryTab();
return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.activity_gallery_tab, null);
gridview = (GridView) view.findViewById(R.id.gridview);
setRetainInstance(true);
startNewAsyncTask();
// new RemoteDataTask(this).execute();
return view;
}

// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
private WeakReference<GalleryTab> fragmentWeakRef;

private RemoteDataTask(GalleryTab gallerytab) {
this.fragmentWeakRef = new WeakReference<GalleryTab>(gallerytab);
}

@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("Gallery");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}

@Override
protected Void doInBackground(Void... params) {
// Create the array

phonearraylist = new ArrayList<GalleryList>();
try {
for (int i = 0; i <= 6; i++) {
GalleryList map = new GalleryList();
map.setGallery("http://oi39.tinypic.com/21oydxs.jpg");
// System.out.println("PRINT!!!!-- "+ i);
phonearraylist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// if (this.fragmentWeakRef.get() != null) {
adapter = new GridViewAdapter(getActivity(), phonearraylist);
// System.out.println("PRINT SIZE -- "+ phonearraylist.size());
gridview.setAdapter(adapter);

mProgressDialog.dismiss();
// }
}
}

private void startNewAsyncTask() {
RemoteDataTask asyncTask = new RemoteDataTask(this);
this.asyncTaskWeakRef = new WeakReference<RemoteDataTask>(asyncTask);
asyncTask.execute();
}
}

My GridViewAdapter Class:-

public class GridViewAdapter extends BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ImageLoader imageLoader;
private List<GalleryList> galleryArraylist = null;
private ArrayList<GalleryList> arraylist;

public GridViewAdapter(Context context, List<GalleryList> phonearraylist) {
this.context = context;
this.galleryArraylist = phonearraylist;
inflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<GalleryList>();
this.arraylist.addAll(phonearraylist);
imageLoader = new ImageLoader(context);
}

public class ViewHolder {
ImageView phone;
}

@Override
public int getCount() {
return galleryArraylist.size();
}

@Override
public Object getItem(int position) {
return galleryArraylist.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.gridview_item, null);
// Locate the ImageView in gridview_item.xml
holder.phone = (ImageView) view.findViewById(R.id.phone);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into ImageView
imageLoader.DisplayImage(galleryArraylist.get(position).getGallery(),
holder.phone);
// Listen for GridView Item Click
view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data phone
intent.putExtra("gallery",
(galleryArraylist.get(position).getGallery()));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return view;
}
}

最佳答案

检查 list 中的必要权限。

关于android - Imageloader 没有在真实设备上加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18416526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com