Image content can not read during zoom

﹥>﹥吖頭↗ 提交于 2019-12-11 05:50:17

问题


When i zoom image by pinch , i can not read the content of image . This is my code:-

if(!(it.isEmpty() )) {
    Collections.sort(it); 
    for(int i=0; i<it.size();i++) {
        ImageViewTouch imageView = new ImageViewTouch(GalleryTouchTestActivity.this);
        imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    final Options options = new Options();
     options.outHeight = (int) scaleHeight; // new smaller height
        options.outWidth = (int) scaleWidth;   // new smaller width
       options.inScaled = true;
        options.inPurgeable = true;
        options.inSampleSize = 8;
        String photoURL = it.get(i);

        Bitmap bitmap = BitmapFactory.decodeFile(photoURL,options);
        imageView.setImageBitmap(bitmap);

        arrayAdapter.add(imageView); 
    }

May any one help me . i have checked it many items but unable to read content . thank you for checking this code and giving me your important time

thank you


回答1:


try to change value as 2 here

options.inSampleSize = 2;



回答2:


use this function instead of BitmapFactory.decodeFile(photoURL,options);

public Bitmap getBitmaplarge(String url,int size) 
{
    File f=fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFilelarge(f, size);
    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);
        Utils2.CopyStream(is, os);
        os.close();
        bitmap = decodeFilelarge(f, 800);
        return bitmap;
    } catch (Exception ex)
    {
       ex.printStackTrace();
       return null;
    }
}

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFilelarge(File f,int size)
{
    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=size;
        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;
}

remove your oprion variable. this fun will automatically adjust that..

package com.lazyloading;

import java.io.File; import android.content.Context;

public class FileCache {

private File cacheDir;

public FileCache(Context context)
{
    //Find the dir to save cached images
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))

        cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"SingpostCache");
    else

        cacheDir=context.getCacheDir();

    if(!cacheDir.exists())
        cacheDir.mkdirs();
}

public File getFile(String url)
{
    //I identify images by hashcode. Not a perfect solution, good for the demo.
    String filename=String.valueOf(url.hashCode());
    //Another possible solution (thanks to grantland)
    //String filename = URLEncoder.encode(url);
    File f = new File(cacheDir, filename);
    return f;

}

public void clear()
{
    File[] files=cacheDir.listFiles();
    if(files==null)
        return;
    for(File f:files)
        f.delete();
}

}

make object of filecache class as FileCache fileCache=new FileCache(context);

and copystream fun

public static void CopyStream(InputStream is, OutputStream os)
{
    final int buffer_size=1024;
    try
    {
        byte[] bytes=new byte[buffer_size];
        for(;;)
        {
          int count=is.read(bytes, 0, buffer_size);
          if(count==-1)
              break;
          os.write(bytes, 0, count);
        }
    }
    catch(Exception ex){}
}



回答3:


You can use WebView for this:

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file:/"+ base + "/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadData(html, "text/html","utf-8");


来源:https://stackoverflow.com/questions/13151970/image-content-can-not-read-during-zoom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!