Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/147.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
Android-从SD卡将位图转换为可绘制_Android_Bitmap_Android Drawable - Fatal编程技术网

Android-从SD卡将位图转换为可绘制

Android-从SD卡将位图转换为可绘制,android,bitmap,android-drawable,Android,Bitmap,Android Drawable,嘿,当我将位图从SD卡转换为可绘图时,在调整大小方面出现了一些奇怪的情况: String sdDir = Environment.getExternalStorageDirectory().getPath(); String filename = "test.png"; //420px X 420px Drawable tmpDraw; Bitmap tmpBit = BitmapFactory.decodeFile(sdDir+filename); Log.e("BAH","Bitmap h

嘿,当我将位图从SD卡转换为可绘图时,在调整大小方面出现了一些奇怪的情况:

String sdDir = Environment.getExternalStorageDirectory().getPath();
String filename = "test.png"; //420px X 420px
Drawable tmpDraw;
Bitmap tmpBit = BitmapFactory.decodeFile(sdDir+filename);

Log.e("BAH","Bitmap height:"+tmpBit.getHeight()); //420

tmpDraw = (Drawable) new BitmapDrawable(getResources(),tmpBit);
int height = tmpDraw.getMinimumHeight();

Log.e("BAH","Drawable height:"+height); //420

我假设我需要一个参考点来缩放?如果一个文件是从SD卡读取的,它是否被归类为MDPI,这就是为什么它会被重新缩放?我目前正在使用Nexus7来测试TVDPI。我希望进行缩放,就好像位图在HDPI中一样

如有任何建议,将不胜感激

我想我已经破解了它:

String sdDir = Environment.getExternalStorageDirectory().getPath();
String filename = "test.png"; //420px X 420px
Drawable tmpDraw;

BitmapFactory.Options options = new BitmapFactory.Options();
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
options.inDensity = 240; //Being DPI for HDPI
options.inTargetDensity=metrics.densityDpi; //Being current DPI
//inDensity/inTargetDensity are a ratio

Bitmap tmpBit = BitmapFactory.decodeFile(sdDir+filename,options);

Log.e("BAH","Bitmap height:"+tmpBit.getHeight()); //373 Correct for TVDPI

tmpDraw = (Drawable) new BitmapDrawable(getResources(),tmpBit);
int height = tmpDraw.getMinimumHeight();

Log.e("BAH","Drawable height:"+height); //373 Correct for TVDPI

这是一种可以接受的方法吗?

是的,您应该将
资源
传递给
BitmapDrawable
构造函数,这样它就不会把事情搞砸。

您到底想要什么?是否要防止缩放,并将高度设置为420而不是315?我希望缩放时SD卡上的位图采用HDPI格式。在位图或可绘制级别。半回答。tmpDraw=(可绘制)新的位图可绘制(getResources(),tmpBit);不产生从位图到可绘制的重缩放。假设SD卡为HDPI格式,我如何使初始位图进行缩放?将为您提供答案,因为您确实指出了位图和可绘制之间的缩放问题。非常感谢。