Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-createScaledBitmap问题_Java_Android_Bitmapfactory - Fatal编程技术网

Java Android-createScaledBitmap问题

Java Android-createScaledBitmap问题,java,android,bitmapfactory,Java,Android,Bitmapfactory,我正在使用以下代码: private Bitmap decodeFile(File f) { try { // decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f), null,

我正在使用以下代码:

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 = 75;
        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;
        o2.inScaled = false;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}



public Drawable getProfileImageJSON(String id) {
    String url = "http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2011/05/image_scaling_android.jpg";
    Bitmap b;
    MemoryCache memoryCache = new MemoryCache();

    try {

        Bitmap memCacheBitmap = memoryCache.get(url);

        if (memCacheBitmap != null)
            b = memCacheBitmap;

        else {
            FileCache fileCache = new FileCache(context);
            File f = fileCache.getFile(url);
            Bitmap fileCacheBitmap = decodeFile(f);

            if (fileCacheBitmap != null)
                b = fileCacheBitmap;
            else {
                InputStream is = (InputStream) new URL(url).getContent();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                b = decodeFile(f);
                memoryCache.put(url, b);
            }
        }

        b.setDensity(Bitmap.DENSITY_NONE);


        Drawable d = new BitmapDrawable(context.getResources(),
                Bitmap.createScaledBitmap(b, 75, 75, true));


        // Drawable d = Drawable.createFromStream(is, "src");

        if (d.getIntrinsicHeight() == 0 || d.getIntrinsicHeight() == -1) {
            d = context.getResources().getDrawable(
                    R.drawable.defaultprofile);
            return d;
        }

        else {
            return d;
        }

    }

    catch (Exception e) {
        // Drawable d2 = getResources().getDrawable( R.drawable.icon );


        Drawable d = context.getResources().getDrawable(
                R.drawable.defaultprofile);
        return d;
    }
}
{ Resources=context.getResources(); DisplayMetrics=resources.getDisplayMetrics(); 浮动px=dp*(公制密度dpi/160f)/1.5f; 返回px; }使用以下方法:-

 Drawable d = new BitmapDrawable(context.getResources(),
                Bitmap.createScaledBitmap(b, ((int) convertDpToPixel(75)),  ((int) convertDpToPixel(75)), true));
函数convertDpToPixel

public float convertDpToPixel(float dp, Activity context)
{
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;
}
您的静态值根据每个设备的ppi而变化,因此请使用上述代码

更多信息:-


使用DP值(与密度无关的像素)。在/res/values/dimens.xml中定义所需的大小,并从中读取它们。还有metrics.density,它等于(metrics.densityDpi/160f)Hemant,反之亦然。现在图像看起来更大了(~100px*100px)。默认图像是75*75 x,但加载的图像更大。Hemant-因此它最终工作了。我必须把px值除以1.5。
public float convertDpToPixel(float dp, Activity context)
{
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;
}