編輯:關於Android編程
package
barry.android.c;
import
java.io.File;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
android.app.Activity;
import
android.content.Intent;
import
android.graphics.Bitmap;
import
android.os.Bundle;
import
android.os.Environment;
import
android.provider.MediaStore;
import
android.util.Log;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ImageView;
/**
*
* @title MyCaremaActivity.java
*
@package barry.android.c
*
@email [email protected]
*
@author szh
*
@update 2013-1-11 上午9:27:03
*
@description
*/
public
class
MyCaremaActivity
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button
button = (Button) findViewById(R.id.button);
button.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
Intent
intent =
new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//open
camera
startActivityForResult(intent,
1
);
}
});
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
super
.onActivityResult(requestCode,
resultCode, data);
if
(resultCode == Activity.RESULT_OK) {
String
sdStatus = Environment.getExternalStorageState();
if
(sdStatus.equals(Environment.MEDIA_MOUNTED)) {
//
檢測sd是否可用
Log.v(
"TestFile"
,
sdStatus+
"SD
card is not avaiable/writeable right now."
);
}
Bundle
bundle = data.getExtras();
Bitmap
bitmap = (Bitmap) bundle.get(
"data"
);
//
獲取相機返回的數據,並轉換為Bitmap圖片格式
FileOutputStream
b =
null
;
File
file =
new
File(
"/sdcard/myImage/"
);
file.mkdirs();
//
創建文件夾
String
fileName =
"/sdcard/myImage/111.jpg"
;
try
{
b
=
new
FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG,
100
,
b);
//
把數據寫入文件
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
finally
{
try
{
b.flush();
b.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
((ImageView)
findViewById(R.id.imageView)).setImageBitmap(bitmap);
//
將圖片顯示在ImageView裡
}
}
}
package
com.hipikids.mycamerademo;
import
java.io.BufferedOutputStream;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
android.app.Activity;
import
android.content.pm.ActivityInfo;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.PixelFormat;
import
android.hardware.Camera;
import
android.hardware.Camera.AutoFocusCallback;
import
android.hardware.Camera.PictureCallback;
import
android.os.Bundle;
import
android.view.SurfaceHolder;
import
android.view.SurfaceView;
import
android.view.View;
import
android.view.Window;
import
android.view.SurfaceHolder.Callback;
import
android.view.View.OnClickListener;
/**
*
* @title MainActivity.java
*
*
@package com.hipikids.mycamerademo
*
@email [email protected]
*
@author szh
*
@update 2013-1-10 下午5:21:00
*
@description
*/
public
class
MainActivity
extends
Activity
implements
Callback,
OnClickListener,
AutoFocusCallback {
SurfaceView
mySurfaceView;
//
surfaceView聲明
SurfaceHolder
holder;
//
surfaceHolder聲明
Camera
myCamera;
//
相機聲明
String
filePath =
"/sdcard/wjh.jpg"
;
//
照片保存路徑
boolean
isClicked =
false
;
//
是否點擊標識
//
創建jpeg圖片回調數據對象
PictureCallback
jpeg =
new
PictureCallback() {
@Override
public
void
onPictureTaken(
byte
[]
data, Camera camera) {
//
TODO Auto-generated method stub
try
{
//
獲得圖片
Bitmap
bm = BitmapFactory.decodeByteArray(data,
0
,
data.length);
File
file =
new
File(filePath);
BufferedOutputStream
bos =
new
BufferedOutputStream(
new
FileOutputStream(file));
bm.compress(Bitmap.CompressFormat.JPEG,
100
,
bos);
//
將圖片壓縮到流中
bos.flush();
//
輸出
bos.close();
//
關閉
}
catch
(Exception e) {
e.printStackTrace();
}
}
};
/**
Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//
無標題
//
設置拍攝方向
this
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
//
獲得控件
mySurfaceView
= (SurfaceView) findViewById(R.id.surface);
//
獲得句柄
holder
= mySurfaceView.getHolder();
//
添加回調
holder.addCallback(
this
);
//
設置類型
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//
設置監聽
mySurfaceView.setOnClickListener(
this
);
}
@Override
public
void
surfaceChanged(SurfaceHolder holder,
int
format,
int
width,
int
height) {
//
TODO Auto-generated method stub
//
設置參數並開始預覽
Camera.Parameters
params = myCamera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);
params.setPreviewSize(
640
,
480
);
myCamera.setParameters(params);
myCamera.startPreview();
//
open display
}
@Override
public
void
surfaceCreated(SurfaceHolder holder) {
//
TODO Auto-generated method stub
//
開啟相機
if
(myCamera ==
null
)
{
myCamera
= Camera.open();
//
this method is a static method
try
{
myCamera.setPreviewDisplay(holder);
//
connection SurfaceHolder
}
catch
(IOException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public
void
surfaceDestroyed(SurfaceHolder holder) {
//
TODO Auto-generated method stub
//
關閉預覽並釋放資源
myCamera.stopPreview();
//
close display
myCamera.release();
myCamera
=
null
;
}
@Override
public
void
onClick(View v) {
//
TODO Auto-generated method stub
if
(!isClicked) {
myCamera.autoFocus(
this
);
//
自動對焦
isClicked
=
true
;
}
else
{
myCamera.startPreview();
//
開啟預覽
isClicked
=
false
;
}
}
@Override
public
void
onAutoFocus(
boolean
success, Camera camera) {
//
TODO Auto-generated method stub
if
(success) {
//
設置參數,並拍照
Camera.Parameters
params = myCamera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);
params.setPreviewSize(
640
,
480
);
myCamera.setParameters(params);
myCamera.takePicture(
null
,
null
,
jpeg);
}
}
}
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
/>
"android.permission.CAMERA"
/>
計算每個線程的下載起始終止位置公式如下文件讀寫方式4中類型工程源碼目錄package cn.itcast.download;import java.io.File;imp
首先先上一張開發效果圖:效果是模仿微信的發送位置,項目的框架上采用mvp模式,並用了我自己寫的一套lib,地址ndileber首先界面上的開發,我簡單粘貼代碼(笑)act
遇到一個問題:昨天模擬器工作還正常,今天eclipse就識別不了了。後來發現是360手機助手占用了5555端口造成的,我就納悶了,平時這個也不是自動啟動,今天就啟動了。廢
隨著微信微博等社會化媒體的火熱,第三方登錄迅速成為一種快捷注冊的方式,社會化分享也成為一種知識快速傳播的渠道。在移動端,幾乎大多數應用都接入了第三方登錄或者分享組件,尤其