Android 图片的读取 缩放 保存

注意AndroidManifest配置权限:(在SD卡写入数据)

 

代码:

从另一个Activity 上传入byte型的数据,通过 bmp = BitmapFactory.decodeByteArray(txInfo, 0, txInfo.length);生成位图。

然后有用到ZoomControls图片缩放,最后可以保存图片到sd卡。

package com.login.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ZoomControls;

public class List_tx extends Activity {

private ImageView imageView1;
private ZoomControls zoom;
private Button saveButton;
private float scaleWidth = 1;
private float scaleHeight = 1;
private Bitmap bmp;
private int zooms = 0;
private ProgressDialog m_Dialog = null;
private Handler handler = new Handler();
private String xh = "";

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_tx);
Bundle b = this.getIntent().getExtras();
byte[] txInfo = b.getByteArray("txInfo");
xh = b.getString("xh");

saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(saveClick);
imageView1 = (ImageView) findViewById(R.id.imageView1);
bmp = BitmapFactory.decodeByteArray(txInfo, 0, txInfo.length);// 生成位图
imageView1.setImageBitmap(bmp);

zoom = (ZoomControls) findViewById(R.id.zoomcontrol);
zoom.setIsZoomInEnabled(true);
zoom.setIsZoomOutEnabled(true);

// 图片放大
zoom.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
zoom.setIsZoomOutEnabled(true);
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
// 设置图片放大但比例
double scale = 1.25;
// 计算这次要放大的比例
if (zooms >= -3 && zooms < 6) {
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
// 产生新的大小但Bitmap对象
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
bmpHeight, matrix, true);
imageView1.setImageBitmap(resizeBmp);
zooms++;
} else
zoom.setIsZoomInEnabled(false);

}

});
// 图片减小
zoom.setOnZoomOutClickListener(new OnClickListener() {

public void onClick(View v) {
zoom.setIsZoomInEnabled(true);
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
// 设置图片放大但比例
double scale = 0.8;
// 计算这次要放大的比例
if (zooms > -3 && zooms <= 6) {
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
// 产生新的大小但Bitmap对象
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
bmpHeight, matrix, true);
imageView1.setImageBitmap(resizeBmp);
zooms--;
} else
zoom.setIsZoomOutEnabled(false);
}

});

}

// 保存
OnClickListener saveClick = new OnClickListener() {

public void onClick(final View v) {
m_Dialog = ProgressDialog.show(List_tx.this, "请稍后...", "图像保存中...",
true);

new Thread(new Runnable() {

public void run() {
File file = new File("/sdcard/" + xh + ".jpeg");
try {
FileOutputStream out = new FileOutputStream(file);
if (bmp.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
out.flush();
out.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("FileNotFoundException");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("FileNotFoundException");
e.printStackTrace();
}

// 更新界面
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"头像保存在" + "/sdcard/" + xh + ".jpeg",
Toast.LENGTH_SHORT).show();
}
});
m_Dialog.dismiss();
}
}).start();
}

};

}

(from 当年的,已经不存在的,我的百度空间)

Android 再按一次退出 的实现


private static Boolean isExit = false;

private static Boolean hasTask = false;

Timer tExit = new Timer();

TimerTask task = new TimerTask() {
public void run() {

isExit = false;

hasTask = true;

}

};

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK

&& event.getAction() == KeyEvent.ACTION_DOWN) {

if (isExit == false) {

isExit = true;
Toast.makeText(getApplicationContext(), "再按一次退出程序",

Toast.LENGTH_SHORT).show();

if (!hasTask) {

tExit.schedule(task, 2000);

}

} else {

finish();

System.exit(0);

}

return false;

}

return super.onKeyDown(keyCode, event);

}
(from 当年的,已经不存在的,我的百度空间)

Android隐藏状态栏和标题栏

方法1:
修改AndroidManifest.xml
隐藏标题栏:android:theme=”@android:style/Theme.NoTitleBar”.

隐藏状态栏(全屏):android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”.

方法2:
修改代码(setContentView(R.layout.main); 之前)

super.onCreate(savedInstanceState);
//隐藏标题栏

requestWindowFeature(Window.FEATURE_NO_TITLE);
//隐藏状态栏(全屏)

getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);

setContentView(R.layout.main);

注:
建议使用方法1,方法2会在开始时候短暂的显示标题栏。

(from 当年的,已经不存在的,我的百度空间)

使用Application在多个Activity间共享对象

在写一个网页登录程序时,我需要在多个Activity之间传递cookies,但是发现实现比较困难,作为List的cookies并不是基本数据类型,似乎还需要实现Serializable接口,显然不太实际。
两种基本的传递:

1  利用Intent传递数据

传递数据的Activity中:a.activiy

Intent intent = new Intent();
intent.putExtra("name","Jon");//在Intent中加入键值对数据。键:name,值:Jon
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);

在取出数据的Activity中:b.activity

Intent intent = getIntent();//获得传过来的Intent。
String value = intent.getStringExtra("name");//根据键name取出值

2 利用Bundle传递数据

传递数据的Activity:a.activity

Intent intent = new Intent();
Bundle myBundle = new Bundle();
myBundle.putString("Key_Name","Tom");
intent.putExtras(myBundle);
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);

取出数据的Activity:b.activity

Bundle getBundle = getIntent().getExtras();
String value = getBundle.getString(“Key_Name”);

3 利用Application 共享

package com.login.main;

import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Application;

public class MyApp extends Application {

private DefaultHttpClient mHttpClient;

public DefaultHttpClient getmHttpClient() {

return mHttpClient;

}

public void setmHttpClient(DefaultHttpClient mHttpClient) {

this.mHttpClient = mHttpClient;

}

}

a.java
//将对象放入app中

MyApp myApp = (MyApp) getApplication();

myApp.setmHttpClient(mHttpClient);

b.java
//取值

MyApp myApp = (MyApp) getApplication();

mHttpClient = myApp.getmHttpClient();

android的(View v)错误

今天在写Intent时一直错误,
代码是这样的

package h.helloworld;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class helloactivity extends Activity {

/** Called when the activity is first created. */

private Button myBut=null;
private TextView myTxt=null;

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTxt=(TextView)findViewById(R.id.myTxt);
myBut=(Button)findViewById(R.id.myBut);
myBut.setText("点我");
myBut.setOnClickListener( new MybuttonL());
}

class MybuttonL implements OnClickListener{

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent=new Intent();
intent.setClass(helloactivity.this, helloactivity2.class);
helloactivity.this.startActivity(intent);

}

}

}

默认的onClick是onClick(DialogInterface dialog, int which),一直是报错,
后来看了很久原来是自动导入的包错了
本来是:

import Android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

应该改为:

import android.view.View;
import android.view.View.OnClickListener;

(from 当年的,已经不存在的,我的百度空间)