标签: bitmapfactory

从文件路径创建位图/ Drawable

我正在尝试从现有文件路径创建一个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

我究竟做错了什么?有人可以帮帮我吗?

java android bitmapfactory android-drawable

76
推荐指数
3
解决办法
14万
查看次数

如何在Android中解决java.lang.OutOfMemoryError问题

尽管我在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)

问题是什么?如果您需要有关代码的其他信息,我可以添加它.谢谢!

android bitmap out-of-memory setbackground bitmapfactory

65
推荐指数
4
解决办法
11万
查看次数

如何在Android中的蓝牙打印机上打印图像?

我必须在热蓝牙打印机上打印一些数据,我正在这样做:

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)

java android bytearray bitmap bitmapfactory

49
推荐指数
4
解决办法
6万
查看次数

最佳使用BitmapFactory.Options.inSampleSize来提高速度

感谢Schermvliegeranddev.org上提出这个问题,

我只是把他的问题复制到SO,因为没有人在另一个网站上回复,我也面临同样的问题.

我想知道BitmapFactory.Options.inSampleSize关于显示图像的速度的最佳用途是什么.
文档提到使用2的幂的值,所以我正在使用2,4,8,16等.

我想知道的是:

  1. 我应该重新采样到仍然大于屏幕分辨率的最小尺寸,还是应该采样到足够大的尺寸以避免OutOfMemoryError
  2. 如何在不耗尽内存的情况下计算仍然可以显示的图像的最大尺寸?图像的颜色深度是否也起作用,显示的深度是什么?
  3. 通过两种机制(BitmapFactory对于较大的文件,setImageURI()对于较小的文件)显示图像是否有效.我正在使用一种ImageSwitcher方式.
  4. 它是否有助于创建Bitmap,BitmapFactory.OptionsinTempStorage在应用程序的开头或只对动态创建它们,在需要的时候?

android bitmap bitmapfactory

22
推荐指数
1
解决办法
2万
查看次数

使用BitmapFactory.Options.inBitmap进行位图分配会抛出IllegalArgumentException

我得到了下一个异常:当设置为true 时,问题解码到现有位图inBitmap ;

引起:java.lang.IllegalArgumentException:
在android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:460)解码到现有位图时出现问题
...

有趣的是,在运行时,相同的代码在不同的地方失败:

  • API:4.4.2,Nexus 4
  • API:4.3.1,三星s3

这是我的代码,它是这个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)

android garbage-collection bitmap bitmapfactory

21
推荐指数
1
解决办法
8917
查看次数

Android:decodeFile始终为内部存储中的文件返回null

我有一个文件本地保存到应用程序的私有存储中.我已经验证它存在,但每当我调用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)

android bitmapfactory

19
推荐指数
2
解决办法
4万
查看次数

Android如何使用BitmapFactory选项缩放图像

我想用BitmapFactory.decodeFile(路径,选项)解码SD卡中的图像.
我还希望将大于2048x2048像素的图像缩小到最多2048x2048(保持比例).
我可以在获取位图后手动执行此操作,但除了已分配的字节之外,还需要分配大量字节.
我应该如何设置我的BitmapFactory.Options对象以获得该效果?
谢谢

android decode bitmap decoding bitmapfactory

18
推荐指数
2
解决办法
4万
查看次数

bitmap.compress(Bitmap.CompressFormat.PNG,0,fOut)使图像大小比原始大

我想将从相机拍摄的图像压缩为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)更大

我错过了什么?这是减少图像大小的最佳方法吗?

谢谢

compression android image-resizing bitmapfactory

17
推荐指数
1
解决办法
4万
查看次数

使用Bitmap.Config.RGB_565将内存中的位图转换为位图

我有一个加载的位图,我想转换为将配置设置为Bitmap.Config.RGB_565.Bitmap在Bitmap已经加载到内存后,是否有一种简单的方法将a 转换为此配置?例如,下面我有一个从应用程序资源解码的位图,但是,我如何将已经加载的转换BitmapRGB_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)

rgb android bitmap bitmapfactory

16
推荐指数
2
解决办法
2万
查看次数

在缩放加载之前,从实际Uri预先猜测Bitmap的大小

如果你有一个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 android-camera bitmapfactory

14
推荐指数
1
解决办法
5994
查看次数