Android 如何调用decode函数对图像进行解码
如何使用解码功能对图像进行压缩和解码?如何调用解码函数 这一行:Android 如何调用decode函数对图像进行解码,android,Android,如何使用解码功能对图像进行压缩和解码?如何调用解码函数 这一行: image = decodeFile(getResources(),R.drawable.my); 显示一个错误: SQLiteDemoActivity类型中的decodeFile(File)方法不适用于参数(参考资料,int) 守则: ArrayList<Contact> imageArry = new ArrayList<Contact>(); ContactImageAdapter adapt
image = decodeFile(getResources(),R.drawable.my);
显示一个错误:
SQLiteDemoActivity类型中的decodeFile(File)方法不适用于参数(参考资料,int)
守则:
ArrayList<Contact> imageArry = new ArrayList<Contact>();
ContactImageAdapter adapter;
Button BrowseButton;
Button BrowseButton2;
DataBaseHandler db;
public static Bitmap image ;
public static byte[] imageInByte;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DataBaseHandler(this);
BrowseButton=(Button)findViewById(R.id.BrowseButton);
BrowseButton2=(Button)findViewById(R.id.BrowseButton2);
adapter = new ContactImageAdapter(this, R.layout.screen_list, imageArry);
BrowseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
< this line show error---> image = decodeFile(getResources(),R.drawable.my);
//BitmapFactory.decodeFile(photo.toString(),options);
image.compress(CompressFormat.JPEG, 100, stream);
imageInByte = stream.toByteArray();
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("FaceBook", imageInByte));
}
});
BrowseButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName() + " ,Image: " + cn.getImage();
//Writing Contacts to log
Log.d("Result: ", log);
//add contacts data in arrayList
imageArry.add(cn);
}
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
try {
stream.close();
stream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
});
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
ArrayList imageArry=new ArrayList();
接触式图像适配器;
按钮浏览按钮;
按钮浏览按钮2;
数据库处理程序数据库;
公共静态位图图像;
公共静态字节[]imageInByte;
ByteArrayOutputStream=新建ByteArrayOutputStream();
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db=新的DataBaseHandler(此);
BrowseButton=(按钮)findViewById(R.id.BrowseButton);
BrowseButton2=(按钮)findViewById(R.id.BrowseButton2);
适配器=新的ContactImageAdapter(此,R.layout.screen\u列表,imageArry);
BrowseButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
<此行显示错误--->image=decodeFile(getResources(),R.drawable.my);
//BitmapFactory.decodeFile(photo.toString(),选项);
压缩(CompressFormat.JPEG,100,流);
imageInByte=stream.toByteArray();
日志d(“插入:”,“插入..”);
db.addContact(新联系人(“FaceBook”,imageInByte));
}
});
BrowseButton2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
List contacts=db.getAllContacts();
用于(联系人cn:联系人){
字符串log=“ID:+cn.getID()+”名称:“+cn.getName()+”,图像:“+cn.getImage()”;
//将联系人写入日志
Log.d(“结果:”,Log);
//在arrayList中添加联系人数据
imageArry.add(中国);
}
ListView数据列表=(ListView)findViewById(R.id.list);
dataList.setAdapter(适配器);
试一试{
stream.close();
流=空;
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<所需尺寸| |高度\u tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
scale++;
}
使用BitmapFatory.decodeResource(…)
而不是decodeFile(…)