안드로이드 개발 질문/답변
(글 수 45,052)
/////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
import android.app.*;
import android.content.*;
import android.hardware.*;
import android.hardware.Camera.*;
import android.net.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
public class C25_Camera extends Activity {
MyCameraSurface mSurface;
Button mShutter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c25_camera);
mSurface = (MyCameraSurface)findViewById(R.id.preview);
// 오토 포커스 시작
findViewById(R.id.focus).setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mShutter.setEnabled(false);
mSurface.mCamera.autoFocus(mAutoFocus);
}
});
// 사진 촬영
mShutter = (Button)findViewById(R.id.shutter);
mShutter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mSurface.mCamera.takePicture(null, null, mPicture);
}
});
}
// 포커싱 성공하면 촬영 허가
AutoFocusCallback mAutoFocus = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
mShutter.setEnabled(success);
}
};
// 사진 저장.
PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
String sd = Environment.getExternalStorageDirectory().getAbsolutePath();
String path = sd + "/cameratest.jpg";
File file = new File(path);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
} catch (Exception e) {
Toast.makeText(C25_Camera.this, "파일 저장 중 에러 발생 : " +
e.getMessage(), 0).show();
return;
}
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.parse("file://" + path);
intent.setData(uri);
sendBroadcast(intent);
Toast.makeText(C25_Camera.this, "사진 저장 완료 : " + path, 0).show();
mSurface.mCamera.startPreview();
}
};
}
// 미리보기 표면 클래스
class MyCameraSurface extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
public MyCameraSurface(Context context, AttributeSet attrs) {
super(context, attrs);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// 표면 생성시 카메라 오픈하고 미리보기 설정
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
// 표면 파괴시 카메라도 파괴한다.
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
// 표면의 크기가 결정될 때 최적의 미리보기 크기를 구해 설정한다.
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = mCamera.getParameters();
List<Size> arSize = params.getSupportedPreviewSizes();
if (arSize == null) {
params.setPreviewSize(width, height);
} else {
int diff = 10000;
Size opti = null;
for (Size s : arSize) {
if (Math.abs(s.height - height) < diff) {
diff = Math.abs(s.height - height);
opti = s;
}
}
params.setPreviewSize(opti.width, opti.height);
}
mCamera.setParameters(params);
mCamera.startPreview();
}
}
///////////////////////////////////xml//////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="118dip"
android:layout_height="fill_parent"
android:background="#404040"
>
<Button
android:id="@+id/focus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Focus"
android:textSize="12pt"
android:paddingTop="8pt"
android:paddingBottom="10pt"
/>
<Button
android:id="@+id/shutter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Shutter"
android:textSize="12pt"
android:paddingTop="8pt"
android:paddingBottom="10pt"
/>
</LinearLayout>
<exam.andexam.MyCameraSurface
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
//////////////////////////////////메니페스트////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.ahna"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Ahna"
android:configChanges="orientation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA"/>
</manifest>
////////////////////몇번을 책이랑 똑같이 해도 안되드라구요 혹시 진저브레이드에서는 이소스 안먹히는건가요?
///////////////////해결점 도와주세욤 ㅜㅜ




저장이 안되는 거라면 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 이것이 빠져서
그런가요?