0

こんにちは、URL から画像をダウンロードしてキャッシュに保存します。次に、これらの画像をキャッシュからカルーセル ビューに読み込みます。

しかし問題は、携帯電話の解像度(720X1124)が高いと画像サイズが小さくなることです。

ここで、画像を保存してキャッシュから表示するコードを提供します...

 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(300000000);
            conn.setReadTimeout(300000000);
            conn.setInstanceFollowRedirects(true);
            InputStream inputstream=conn.getInputStream();
            OutputStream outputstream = new FileOutputStream(f);
            Utils.CopyStream(inputstream, outputstream);
            outputstream.close();
            conn.disconnect();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Throwable ex){
           ex.printStackTrace();
           if(ex instanceof OutOfMemoryError)
               memoryCache.clear();
           return null;
        }
    }

   public void getDimension(int width,int height){
       widthScreen=width;
       heightScreen=height;

   }
    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            final int IMAGE_MAX_SIZE = 120000000; // 1.2MP

            BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
            scaleOptions.inJustDecodeBounds = true;
            FileInputStream stream1=new FileInputStream(f);
            BitmapFactory.decodeStream(stream1,null,scaleOptions);
            stream1.close();

         // find the correct scale value as a power of 2.
            int scale = 1;
            while (scaleOptions.outWidth / scale / 2 >= widthScreen
              && scaleOptions.outHeight / scale / 2 >= heightScreen) {
              scale *= 2;
            }


           Bitmap bitmap = null;
            if (scale > 1) {
               scale--;
                // scale to max possible inSampleSize that still yields an image
                // larger than target
               scaleOptions = new BitmapFactory.Options();
               scaleOptions.inSampleSize = scale;
                bitmap = BitmapFactory.decodeStream(stream1, null, scaleOptions);


                int width=widthScreen;
                int height=heightScreen;

                double y=height;
                double x=width;


                Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, (int) x, 
                   (int) y, true);
                bitmap.recycle();
                bitmap = scaledBitmap;

                System.gc();
            } else {
                bitmap = BitmapFactory.decodeStream(stream1);
            }
            if((widthScreen>=450)&& (heightScreen>=750) ) {
                sample=1;
            }
            else{
                sample=2;
            }
            //decode with inSampleSize
            BitmapFactory.Options scalOption = new BitmapFactory.Options();
            scalOption.inSampleSize=sample;
            FileInputStream stream2=new FileInputStream(f);
            Bitmap bitMap=BitmapFactory.decodeStream(stream2, null, scalOption);
            stream2.close();
            return bitMap;
        } catch (FileNotFoundException e) {
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

電話の解像度に応じて画像サイズを大きくする方法。この問題を克服するために、私はさらに何日も疲れました。しかし、うまくいきません。だから私に正しい指示をください....... ありがとう........

4

3 に答える 3

1

この画像ローダー ライブラリで画像を読み込んでみてください

于 2013-07-12T11:15:52.477 に答える
1

さまざまな画面サイズ/解像度のレイアウトを定義する必要があります。複数の画面のサポートを参照してください。

于 2013-07-12T11:06:03.470 に答える
0
public int  calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;


        int inSampleSize = 1;
        if (height < reqHeight || width < reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }

このメソッドをコードに入れ、このメソッドを次のように呼び出します。

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


             Matrix matrix = new Matrix();
             // matrix.postScale(scaleWidth, scaleHeight);
              matrix.postRotate(45);
            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            //calculateInSampleSize1(o, widthScreen,heightScreen);

            o2.inSampleSize=calculateInSampleSize(o, widthScreen,heightScreen);;
            FileInputStream stream2 = new FileInputStream(f);
            o2.inJustDecodeBounds = false;
            Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);

            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
于 2013-08-29T09:05:08.687 に答える