我正在尝试制作一个应用程序,其中图像来自服务器,我想从该图像制作图像滑块。为实现此目的,我引用了here和here,但我收到了错误
我的ImageAdapter.java
public class ImageAdapter extends PagerAdapter {
Context context;
String[] imagUrl={"http://www.vector-eps.com/wp-content/gallery/donald-duck-vectors/thumbs/thumbs_donald-duck-vectors11.jpg",
"http://www.vector-eps.com/wp-content/gallery/donald-duck-vectors/thumbs/thumbs_donald-duck-vectors11.jpg",
"http://www.vector-eps.com/wp-content/gallery/donald-duck-vectors/thumbs/thumbs_donald-duck-vectors11.jpg"};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return imagUrl.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
ImageLoader imgLoader = new ImageLoader(context);
int loader = R.drawable.ic_cancel;
imgLoader.DisplayImage(imagUrl[position], loader, imageView );
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
这里是ImageLoader.java
public class ImageLoader {
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context){
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
int stub_id = R.drawable.ic_cancel;
public void DisplayImage(String url, int loader, ImageView imageView)
{
stub_id = loader;
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null)
imageView.setImageBitmap(bitmap);
else
{
queuePhoto(url, imageView);
imageView.setImageResource(loader);
}
}
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);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
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();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
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;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
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;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
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() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
Activity a=(Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
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();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
}
}
LogCat是
09-05 14:59:49.765 8364-8364/com.example.pitech09.slidee I/art: Late-enabling -Xcheck:jni
09-05 14:59:49.795 8364-8372/com.example.pitech09.slidee E/art: Failed sending reply to debugger: Broken pipe
09-05 14:59:49.795 8364-8372/com.example.pitech09.slidee I/art: Debugger is no longer active
09-05 14:59:50.820 8364-8364/com.example.pitech09.slidee W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-05 14:59:51.071 8364-8416/com.example.pitech09.slidee D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-05 14:59:51.081 8364-8364/com.example.pitech09.slidee D/Atlas: Validating map...
09-05 14:59:51.129 8364-8416/com.example.pitech09.slidee I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016_msm8610_LA.BF.1.1.1_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.25.03.00
Build Date: 02/11/15 Wed
Local Branch:
Remote Branch: quic/LA.BF.1.1.1_rb1.10
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016 + 62ca4eb + acd831d + 9f8b442 + e027a02 + cba30ba + 53c303a + a649d79 + 23e16f8 + 5e97da7 + cbd2a44 + 33d072a + 7aacf06 + 72b33e7 + 28f6f60 + b4c13d8 + NOTHING
09-05 14:59:51.130 8364-8416/com.example.pitech09.slidee I/OpenGLRenderer: Initialized EGL, version 1.4
09-05 14:59:51.148 8364-8416/com.example.pitech09.slidee D/OpenGLRenderer: Enabling debug mode 0
09-05 14:59:51.254 8364-8364/com.example.pitech09.slidee W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
09-05 14:59:58.706 8364-8954/com.example.pitech09.slidee W/System.err: java.io.FileNotFoundException: /storage/emulated/0/TempImages/-1563297368: open failed: EACCES (Permission denied)
09-05 14:59:58.706 8364-8954/com.example.pitech09.slidee W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader.getBitmap(ImageLoader.java:77)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader.access$000(ImageLoader.java:26)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader$PhotosLoader.run(ImageLoader.java:137)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at java.lang.Thread.run(Thread.java:818)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at libcore.io.Posix.open(Native Method)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
09-05 14:59:58.707 8364-8954/com.example.pitech09.slidee W/System.err: ... 10 more
09-05 14:59:58.816 8364-8953/com.example.pitech09.slidee W/System.err: java.io.FileNotFoundException: /storage/emulated/0/TempImages/-1563297368: open failed: EACCES (Permission denied)
09-05 14:59:58.816 8364-8953/com.example.pitech09.slidee W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader.getBitmap(ImageLoader.java:77)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader.access$000(ImageLoader.java:26)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader$PhotosLoader.run(ImageLoader.java:137)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at java.lang.Thread.run(Thread.java:818)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at libcore.io.Posix.open(Native Method)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
09-05 14:59:58.817 8364-8953/com.example.pitech09.slidee W/System.err: ... 10 more
09-05 15:01:41.300 8364-8372/com.example.pitech09.slidee I/art: Debugger is no longer active
09-05 15:07:17.204 8364-8372/com.example.pitech09.slidee I/art: Debugger is no longer active
09-05 15:11:03.084 29993-29993/com.example.pitech09.slidee W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-05 15:11:03.332 29993-30065/com.example.pitech09.slidee D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-05 15:11:03.345 29993-29993/com.example.pitech09.slidee D/Atlas: Validating map...
09-05 15:11:03.458 29993-30065/com.example.pitech09.slidee I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016_msm8610_LA.BF.1.1.1_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.25.03.00
Build Date: 02/11/15 Wed
Local Branch:
Remote Branch: quic/LA.BF.1.1.1_rb1.10
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016 + 62ca4eb + acd831d + 9f8b442 + e027a02 + cba30ba + 53c303a + a649d79 + 23e16f8 + 5e97da7 + cbd2a44 + 33d072a + 7aacf06 + 72b33e7 + 28f6f60 + b4c13d8 + NOTHING
09-05 15:11:03.464 29993-30065/com.example.pitech09.slidee I/OpenGLRenderer: Initialized EGL, version 1.4
09-05 15:11:03.508 29993-30065/com.example.pitech09.slidee D/OpenGLRenderer: Enabling debug mode 0
09-05 15:11:03.513 29993-30006/com.example.pitech09.slidee I/art: Background sticky concurrent mark sweep GC freed 18529(2MB) AllocSpace objects, 9(170KB) LOS objects, 30% free, 5MB/8MB, paused 5.840ms total 113.009ms
09-05 15:11:03.972 29993-30066/com.example.pitech09.slidee W/System.err: java.io.FileNotFoundException: /storage/emulated/0/TempImages/-1563297368: open failed: EACCES (Permission denied)
09-05 15:11:03.972 29993-30066/com.example.pitech09.slidee W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
09-05 15:11:03.972 29993-30066/com.example.pitech09.slidee W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
09-05 15:11:03.972 29993-30066/com.example.pitech09.slidee W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
09-05 15:11:03.972 29993-30066/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader.getBitmap(ImageLoader.java:77)
09-05 15:11:03.972 29993-30066/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader.access$000(ImageLoader.java:26)
09-05 15:11:03.972 29993-30066/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader$PhotosLoader.run(ImageLoader.java:137)
09-05 15:11:03.973 29993-30066/com.example.pitech09.slidee W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
09-05 15:11:03.973 29993-30066/com.example.pitech09.slidee W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-05 15:11:03.973 29993-30066/com.example.pitech09.slidee W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-05 15:11:03.973 29993-30066/com.example.pitech09.slidee W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-05 15:11:03.973 29993-30066/com.example.pitech09.slidee W/System.err: at java.lang.Thread.run(Thread.java:818)
09-05 15:11:03.973 29993-30066/com.example.pitech09.slidee W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
09-05 15:11:03.973 29993-30066/com.example.pitech09.slidee W/System.err: at libcore.io.Posix.open(Native Method)
09-05 15:11:03.975 29993-30066/com.example.pitech09.slidee W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
09-05 15:11:03.975 29993-30066/com.example.pitech09.slidee W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
09-05 15:11:03.975 29993-30066/com.example.pitech09.slidee W/System.err: ... 10 more
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: java.io.FileNotFoundException: /storage/emulated/0/TempImages/-1563297368: open failed: EACCES (Permission denied)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader.getBitmap(ImageLoader.java:77)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader.access$000(ImageLoader.java:26)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at com.example.pitech09.slidee.ImageLoader$PhotosLoader.run(ImageLoader.java:137)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at java.lang.Thread.run(Thread.java:818)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
09-05 15:11:04.088 29993-30067/com.example.pitech09.slidee W/System.err: at libcore.io.Posix.open(Native Method)
09-05 15:11:04.089 29993-30067/com.example.pitech09.slidee W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
09-05 15:11:04.089 29993-30067/com.example.pitech09.slidee W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
09-05 15:11:04.089 29993-30067/com.example.pitech09.slidee W/System.err: ... 10 more
09-05 15:11:15.361 29993-30001/com.example.pitech09.slidee W/art: Suspending all threads took: 15.336ms
我已添加以下权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
答案 0 :(得分:0)
您正在使用Universal Image Loader,因此您已按照此快速设置说明操作,请阅读UIL Quick Setup。 请阅读Android Manifest设置,表明您的应用程序清单必须具有Internet和外部存储权限。所以你必须在AndroidManifest.xml文件中添加两个权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果您正在使用api laval 23,则在使用Universal Image Loader之前必须首先询问运行时权限。请阅读官方文档了解更多详情。 Runtime Permission Documentation