读取到的bitmap,转成buffer并保存

替代图像设置
本文介绍了一个应用程序功能,允许用户选择和设置替代图像用于视频通话。用户可以通过编辑按钮选择一张图片,并将其裁剪为指定尺寸,然后保存到默认路径。此外,还提供了启用和禁用替代图像的功能。

/*
 *  *
 * Corporation and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from BYD Corporation is strictly prohibited.
 *
 * File Name:  bp_log.c
 *
 * General Description: This file implements all the bp panic block log functions.
 *
 *
 *    Date               Author            CR/PR                Reference
 *    ========      ========     ==========      ==========================
 *    2011-1-26    hjh              T601T_P000421     should add "Alternative picture settings" for Video call settings
 *    2011-2-12  hjhT601T_P000903  rehab VT alternative image
 */

package com.android.phone;

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import javax.xml.transform.stream.StreamSource;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.opengl.Visibility;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class AlternativeImage extends Activity {
 public static String VT_ALTERNATIVE_SETTING="vt_alternative_image_url";
 public static String VT_ALTERNATIVE_IMAGE_DEFAULT_URL="/data/data/com.android.phone/temp/default.raw";
 public static String VT_ALTERNATIVE_IMAGE_NEW_URL="/data/data/com.android.phone/temp/new.raw";
 public static String VT_TEMP="/data/data/com.android.phone/temp";
 private int mBitmapSize = 0;
 
 //T601T_P000903 hjh
 public static String VT_IS_ENABLE="vt_alternative_enable";
 Button edit;
 Button unenable;
 Button enable;
 Button reset;
 ImageView mImageView;
 private static final int menuId=Menu.FIRST;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.image_instead);
  edit=(Button)findViewById(R.id.Edit);
  unenable=(Button)findViewById(R.id.Unenable);
  enable=(Button)findViewById(R.id.enable);
  mImageView=(ImageView)findViewById(R.id.imageUrl);
  edit.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
      Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
      imageIntent.setType("image/*");
     
      //T601T_P000903 hjh --begin--
      ComponentName comp = new ComponentName("com.cooliris.media","com.cooliris.media.Gallery");
      imageIntent.setComponent(comp);
      // --end--
     
      imageIntent.putExtra("crop", "true");
      imageIntent.putExtra("aspectX", 1);
      imageIntent.putExtra("aspectY", 1);
      imageIntent.putExtra("outputX", 176);
      imageIntent.putExtra("outputY", 144);
      imageIntent.putExtra("return-data", true);
      startActivityForResult(imageIntent, 1);
   }
  });
  
  unenable.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    putAbleVlue("0");
    updateScreen();
   }
  });
  
  enable.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    putAbleVlue("1");
    updateScreen();
   }
  });
  
  File destDir = new File(VT_TEMP);
    if (!destDir.exists()) {
     destDir.mkdirs();
    }
   File defaultImage=new File(VT_ALTERNATIVE_IMAGE_DEFAULT_URL);
   if(!defaultImage.exists())
   {
   Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.background);
   try
   {
    SaveMyBitmap(bitmap,VT_ALTERNATIVE_IMAGE_DEFAULT_URL);
   }
   catch(IOException e)
   {}
   }

 }
 @Override
 protected void onResume() {
  Log.i("++alternativeImage onResume++","++");
  updateScreen();
  super.onResume();
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0, menuId, 0,R.string.vt_alternative_image_reset);
  return true;
 }
 
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch(item.getItemId())
  {
  case menuId:
   putSrc(VT_ALTERNATIVE_IMAGE_DEFAULT_URL);
   updateScreen();
  }
  return super.onOptionsItemSelected(item);
 }

 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  Log.i("alternativer onActivityResult+++","++++");
  if(resultCode==RESULT_OK)
  {
   final Bitmap bitmap=data.getParcelableExtra("data");
   putSrc(VT_ALTERNATIVE_IMAGE_NEW_URL);
   try
   {
    SaveMyBitmap(bitmap, VT_ALTERNATIVE_IMAGE_NEW_URL);
   }
   catch(IOException e)
   {}
   
  }
  super.onActivityResult(requestCode, resultCode, data);
 }
 public void SaveMyBitmap(Bitmap bitmap,String src) throws IOException
 {
  int width,height;
  width = bitmap.getWidth();
  height = bitmap.getHeight();
  mBitmapSize=width*height*4;
  
  File f=new File(src);
  f.createNewFile();
  try{
   OutputStream fout=new FileOutputStream(f);
//   bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
//   fout.flush();
//   fout.close();
   ByteBuffer mBuf = ByteBuffer.allocate(mBitmapSize);
   Bitmap FuBitmap=bitmap.copy(Bitmap.Config.RGB_565, false);
   FuBitmap.copyPixelsToBuffer(mBuf);
   fout.write(mBuf.array());
   fout.close();
  }
  catch(FileNotFoundException e)
  {
   e.printStackTrace();
  }
  
 }
 public void putSrc(String src)
 {
  Log.i("putSrc++","++");
  if(Settings.System.putString(this.getContentResolver(),
    VT_ALTERNATIVE_SETTING, src))
  {
   Log.i("putSrc+++","++true");
  }
 }
 public void readSrcFromData()
 {
  String src=Settings.System.getString(this.getContentResolver(),VT_ALTERNATIVE_SETTING);
  Log.i("readSrcFromData+++","++src="+src);
  File file=new File(src);
  if(!file.exists())
  {
   file=new File(VT_ALTERNATIVE_IMAGE_DEFAULT_URL);
  }
  byte[] data=new byte[(int)file.length()];
  try{
   InputStream ip=new FileInputStream(file);
   ip.read(data);
   Bitmap bb=Bitmap.createBitmap(176, 144, Bitmap.Config.RGB_565);
   ByteBuffer buf=ByteBuffer.wrap(data);
   bb.copyPixelsFromBuffer(buf);
   mImageView.setImageBitmap(bb);
   ip.close();
  }
  catch(Exception e)
  {}
 }
 
 public void updateScreen()
 {
  readSrcFromData();
  if(readAbleVlueFromDate().equals("1"))
  {
   edit.setVisibility(View.VISIBLE);
   unenable.setVisibility(View.VISIBLE);
   enable.setVisibility(View.GONE);
  }
  else
  {
   edit.setVisibility(View.GONE);
   unenable.setVisibility(View.GONE);
   enable.setVisibility(View.VISIBLE);
  }
 }
 public String readAbleVlueFromDate()
 {
  return Settings.System.getString(this.getContentResolver(),VT_IS_ENABLE);
 }
 
 public void putAbleVlue(String value)
 {
  if(Settings.System.putString(this.getContentResolver(),
    VT_IS_ENABLE, value))
  {
   Log.i("putSrc+++","++true");
  }
 }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值