zoukankan      html  css  js  c++  java
  • The Perfect Solution To Convert Immutable Bitmap To A Mutable Bitmap

    An acceptable solution to convert immutable bitmap to a mutable bitmap ,using RadomAccessFile to save the source bitmap on disk(no ram memory) and then release it, and after that, loading the file info to another bitmap ,the way is perfectly cut off the ram memory use, it just have one bitmap stored in ram memory per time!!!

    >This is the original
    /**
    * Converts a immutable bitmap to a mutable bitmap. This operation doesn't
    * allocates more memory that there is already allocated.
    * @param imgIn - Source image. It will be released, and should not be used more
    * @return a copy of imgIn, but muttable.
    */
    public static Bitmap convertToMutable(Bitmap imgIn) {
    try {
    // this is the file going to use temporally to save the bytes.
    // This file will not be a image, it will store the raw image data.
    // File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.tmp");
    File file = new File(FileUtils.getDiskCacheDir(DataApplication.getContext()) + File.separator + "temp.tmp");

    		// Open an RandomAccessFile
    		// Make sure you have added uses-permission
    		// android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    		// into AndroidManifest.xml file
    		RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    
    		// get the width and height of the source bitmap.
    		int width = imgIn.getWidth();
    		int height = imgIn.getHeight();
    		Config type = imgIn.getConfig();
    
    		// Copy the byte to the file
    		// Assume source bitmap loaded using options.inPreferredConfig =
    		// Config.ARGB_8888;
    		FileChannel channel = randomAccessFile.getChannel();
    		MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
    		imgIn.copyPixelsToBuffer(map);
    		// recycle the source bitmap, this will be no longer used.
    		imgIn.recycle();
    		System.gc();// try to force the bytes from the imgIn to be released
    
    		// Create a new bitmap to load the bitmap again. Probably the memory
    		// will be available.
    		imgIn = Bitmap.createBitmap(width, height, type);
    		map.position(0);
    		// load it back from temporary
    		imgIn.copyPixelsFromBuffer(map);
    		// close the temporary file and channel , then delete that also
    		channel.close();
    		randomAccessFile.close();
    
    		// delete the temp file
    		file.delete();
    
    	} catch (FileNotFoundException e) {
    		e.printStackTrace();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	return imgIn;
    }
  • 相关阅读:
    如何使用SAP Intelligent Robotic Process Automation自动操作Excel
    OpenSAML 使用引导 IV: 安全特性
    Spring Cloud Zuul 网关使用与 OAuth2.0 认证授权服务
    微服务架构集大成者—Spring Cloud (转载)
    Spring Cloud Eureka 服务注册列表显示 IP 配置问题
    使用 Notification API 开启浏览器桌面提醒
    SignalR 中使用 MessagePack 序列化提高 WebSocket 通信性能
    配置 Nginx 的目录浏览功能
    关于 Nginx 配置 WebSocket 400 问题
    Migrate from ASP.NET Core 2.0 to 2.1
  • 原文地址:https://www.cnblogs.com/David-Young/p/6618123.html
Copyright © 2011-2022 走看看