Skip to content Skip to sidebar Skip to footer

Image Content Can Not Read During Zoom

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

Solution 1:

try to change value as 2 here

options.inSampleSize = 2;

Solution 2:

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

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

    //from SD cacheBitmapb= decodeFilelarge(f, size);
    if(b!=null)
        return b;

    //from webtry 
    {
        Bitmap bitmap=null;
        URLimageUrl=newURL(url);
        HttpURLConnectionconn= (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is=conn.getInputStream();
        OutputStreamos=newFileOutputStream(f);
        Utils2.CopyStream(is, os);
        os.close();
        bitmap = decodeFilelarge(f, 800);
        return bitmap;
    } catch (Exception ex)
    {
       ex.printStackTrace();
       returnnull;
    }
}

//decodes image and scales it to reduce memory consumptionprivate Bitmap decodeFilelarge(File f,int size)
{
    try
    {
        //decode image size
        BitmapFactory.Optionso=newBitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(newFileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.finalint 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.Optionso2=newBitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(newFileInputStream(f), null, o2);
    } 
    catch (FileNotFoundException e) {}
    returnnull;
}

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

package com.lazyloading;

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

public class FileCache {

privateFile cacheDir;

publicFileCache(Context context)
{
    //Find the dir to save cached imagesif (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))

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

        cacheDir=context.getCacheDir();

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

publicFilegetFile(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 = newFile(cacheDir, filename);
    return f;

}

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

}

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

and copystream fun

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

Solution 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");

Post a Comment for "Image Content Can Not Read During Zoom"