我正在尝试从现有文件路径创建一个Bitmap或Drawable.
String path = intent.getStringExtra("FilePath");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPreferredConfig = Bitmap.Config.ARGB_8888;
mImg.setImageBitmap(BitmapFactory.decodeFile(path));
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option));
// mImg.setImageDrawable(Drawable.createFromPath(path));
mImg.setVisibility(View.VISIBLE);
mText.setText(path);
Run Code Online (Sandbox Code Playgroud)
但是setImageBitmap()
,setImageDrawable()
没有显示路径中的图像.我打印了路径,mText
它看起来像:/storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg
我究竟做错了什么?有人可以帮帮我吗?
尽管我在drawable文件夹中有一个非常小的图像,我从用户那里得到了这个错误.我没有在代码中使用任何位图功能.至少故意:)
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
at android.content.res.Resources.loadDrawable(Resources.java:3436)
at android.content.res.Resources.getDrawable(Resources.java:1909)
at android.view.View.setBackgroundResource(View.java:16251)
at com.autkusoytas.bilbakalim.SoruEkrani.cevapSecimi(SoruEkrani.java:666)
at com.autkusoytas.bilbakalim.SoruEkrani$9$1.run(SoruEkrani.java:862)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
根据这个stackTrace,我在这一行得到了这个错误('tv'是一个textView):
tv.setBackgroundResource(R.drawable.yanlis);
Run Code Online (Sandbox Code Playgroud)
问题是什么?如果您需要有关代码的其他信息,我可以添加它.谢谢!
我必须在热蓝牙打印机上打印一些数据,我正在这样做:
String message="abcdef any message 12345";
byte[] send;
send = message.getBytes();
mService.write(send);
Run Code Online (Sandbox Code Playgroud)
它适用于文本,但不适用于图像.我想我需要获取byte[]
图像数据.我尝试以这种方式获取图像的数据:
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.qrcode);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
Run Code Online (Sandbox Code Playgroud)
不幸的是,打印机会打印出许多奇怪的字符(大约50厘米的纸张).我不知道如何打印图像.
我想尝试获取位图的像素,然后将其转换为a byte[]
并发送它,但我不知道该怎么做.
谢谢
更新:
经过这么多时间,我这样做:我有一个名为print_image(String file)的方法,它获取我想要打印的图像的路径:
private void print_image(String file) {
File fl = new File(file);
if (fl.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(file);
convertBitmap(bmp);
mService.write(PrinterCommands.SET_LINE_SPACING_24);
int offset = 0;
while (offset < bmp.getHeight()) {
mService.write(PrinterCommands.SELECT_BIT_IMAGE_MODE);
for (int x = 0; x < bmp.getWidth(); ++x) {
for (int k = 0; k < …
Run Code Online (Sandbox Code Playgroud) 感谢Schermvlieger在anddev.org上提出这个问题,
我只是把他的问题复制到SO,因为没有人在另一个网站上回复,我也面临同样的问题.
我想知道BitmapFactory.Options.inSampleSize
关于显示图像的速度的最佳用途是什么.
文档提到使用2的幂的值,所以我正在使用2,4,8,16等.
我想知道的是:
OutOfMemoryError
?BitmapFactory
对于较大的文件,setImageURI()
对于较小的文件)显示图像是否有效.我正在使用一种ImageSwitcher
方式.Bitmap
,BitmapFactory.Options
并inTempStorage
在应用程序的开头或只对动态创建它们,在需要的时候?我得到了下一个异常:当设置为true 时,问题解码到现有位图inBitmap
;
引起:java.lang.IllegalArgumentException:
在android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:460)解码到现有位图时出现问题
...
有趣的是,在运行时,相同的代码在不同的地方失败:
这是我的代码,它是这个DevBytes:Bitmap Allocation视频中显示的副本.
private BitmapFactory.Options options;
private Bitmap reusedBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.image_view);
// set the size to option, the images we will load by using this option
options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inMutable = true;
BitmapFactory.decodeResource(getResources(), R.drawable.img1, options);
// we will create empty bitmap by using the option
reusedBitmap = Bitmap.createBitmap(options.outWidth, options.outHeight, …
Run Code Online (Sandbox Code Playgroud) 我有一个文件本地保存到应用程序的私有存储中.我已经验证它存在,但每当我调用BitmapFactory.decodeFile时它总是返回null.
如果我将文件保存为资源并使用ImageView setImageResource,它总是显示正常.
问题是什么?
这是片段:
filename = "test.png";
if (doesFileExist(filename))
Bitmap bMap = BitmapFactory.decodeFile(filename);
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
Bitmap bMap = BitmapFactory.decodeFile(getFilesDir().getPath()
+ filename);
Run Code Online (Sandbox Code Playgroud) 我想用BitmapFactory.decodeFile(路径,选项)解码SD卡中的图像.
我还希望将大于2048x2048像素的图像缩小到最多2048x2048(保持比例).
我可以在获取位图后手动执行此操作,但除了已分配的字节之外,还需要分配大量字节.
我应该如何设置我的BitmapFactory.Options对象以获得该效果?
谢谢
我想将从相机拍摄的图像压缩为png格式,使它们的尺寸更小,所以我使用这个代码:
compressedPictureFile = new File(imagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
FileOutputStream fOut = new FileOutputStream(compressedPictureFile);
boolean compressed = bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
fOut.flush();
fOut.close();
Run Code Online (Sandbox Code Playgroud)
问题是compressedPictureFile实际上比原始图像(从1 Mb到6Mb)更大
我错过了什么?这是减少图像大小的最佳方法吗?
谢谢
我有一个加载的位图,我想转换为将配置设置为Bitmap.Config.RGB_565
.Bitmap
在Bitmap已经加载到内存后,是否有一种简单的方法将a 转换为此配置?例如,下面我有一个从应用程序资源解码的位图,但是,我如何将已经加载的转换Bitmap
为RGB_565
?我确定这很简单,但是,我对使用Bitmaps很新,经过几个小时的在线查看,遗憾的是我无法找到我需要的东西.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.RGB_565
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.myphoto ,options);
Run Code Online (Sandbox Code Playgroud) 如果你有一个Uri并且你需要它Bitmap
,理论上你可以做到这一点
Bitmap troublinglyLargeBmp =
MediaStore.Images.Media.getBitmap(
State.mainActivity.getContentResolver(), theUri );
Run Code Online (Sandbox Code Playgroud)
但它每次都会崩溃,
所以你这样做.........
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
AssetFileDescriptor fileDescriptor =null;
fileDescriptor =
State.mainActivity.getContentResolver().openAssetFileDescriptor( theUri, "r");
Bitmap actuallyUsableBitmap
= BitmapFactory.decodeFileDescriptor(
fileDescriptor.getFileDescriptor(), null, options);
Utils.Log("'4-sample' method bitmap ... "
+actuallyUsableBitmap.getWidth() +" "
+actuallyUsableBitmap.getHeight() );
Run Code Online (Sandbox Code Playgroud)
这太棒了,是工作的解决方案.
请注意,在当前条件下(2014年),典型的相机尺寸等,"四"因子往往效果很好.但是,最好在Uri上猜测或准确了解图像数据的大小,然后使用它信息,智能地选择那个因素.
简而言之,如何在加载Uri时正确选择该比例因子?
Android专家,这是一个众所周知的问题,有没有解决方案?感谢您的iOS-> Android好友!:)
android ×10
bitmapfactory ×10
bitmap ×6
java ×2
bytearray ×1
compression ×1
decode ×1
decoding ×1
rgb ×1