1

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

4

3 回答 3

0

使用此函数代替 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;
}

删除您的 oprion 变量。这个乐趣会自动调整..

包 com.lazyloading;

导入java.io.File;导入android.content.Context;

公共类文件缓存 {

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();
}

}

将文件缓存类的对象设为 FileCache fileCache=new FileCache(context);

和复制流的乐趣

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){}
}
于 2012-10-31T06:03:06.277 回答
0

您可以WebView为此使用:

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");
于 2012-10-31T12:39:21.090 回答
0

尝试在此处将值更改为 2

options.inSampleSize = 2;
于 2012-10-31T05:55:49.147 回答