androidのカメラで撮影した画像をダイアログに表示したいが上手くいかない
androidのカメラで撮影した画像をダイアログに表示したいがエラー出て思った通りに行きません。
わかる方ご教授をお願いします。
エラー内容
パッケージ名.MainActivity$SampleClickListener$SamplePictureCallback@4288cae8
img_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rltWarning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center"
android:layout_margin="20dp"
android:clickable="true">
<ImageView
android:id="@+id/imgdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_alignParentStart="false" />
MainActivity.java
public class MainActivity extends Activity {
private SQLiteDatabase db;
Button bt;
SurfaceView sv;
SurfaceHolder sh;
Camera cm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int Width = 10;
int Height = 10;
FrameLayout ll = new FrameLayout(this);
//ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new FrameLayout.LayoutParams(
Width, Height));
//ll.setGravity(Gravity.CENTER);
setContentView(ll);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// レイアウトに張り付け
bt = new Button(this);
bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_customize));
bt.setText("撮影");
bt.setWidth(10);
bt.setHeight(10);
sv = new SurfaceView(this);
sh = sv.getHolder();
sh.addCallback(new SampleSurfaceHolderCallback());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity =Gravity.CENTER+200;
///真ん中に設定;
bt.setLayoutParams(params);
ll.addView(sv);
ll.addView(bt);
bt.setOnClickListener(new SampleClickListener());
}
class SampleSurfaceHolderCallback
implements SurfaceHolder.Callback {
public void surfaceCreated(SurfaceHolder holder) {
try {
cm = Camera.open(0);
Camera.Parameters pr = cm.getParameters();
List<Size> ss = pr.getSupportedPictureSizes();
Size s = ss.get(0);
pr.setPictureSize(s.width, s.height);
cm.setParameters(pr);
} catch (Exception e) {
// e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display dp = wm.getDefaultDisplay();
int rt = dp.getRotation();
int d = 0;
switch (rt) {
case Surface.ROTATION_0:
d = 90;
break;
case Surface.ROTATION_90:
d = 0;
break;
case Surface.ROTATION_180:
d = 270;
break;
case Surface.ROTATION_270:
d = 180;
break;
}
cm.setDisplayOrientation(d);
cm.setPreviewDisplay(sv.getHolder());
Camera.Parameters pr = cm.getParameters();
List<Size> previewSizes = cm.getParameters().getSupportedPreviewSizes();
Size size = previewSizes.get(0);
pr.setPreviewSize(size.width, size.height);
cm.setParameters(pr);
cm.startPreview();
} catch (Exception e) {
}
}
public void surfaceDestroyed(SurfaceHolder Holder) {
cm.stopPreview();
cm.release();
}
}
class SampleClickListener implements OnClickListener {
public void onClick(View v) {
if (bt.getVisibility() == View.INVISIBLE) {
// 表示されている時に非表示に
bt.setVisibility(View.VISIBLE);
}
cm.takePicture(null, null, new SamplePictureCallback());
}
class SamplePictureCallback implements PictureCallback {
public void onPictureTaken(byte[] data, Camera c) {
try{
Toast.makeText(getApplicationContext(), "撮影完了。", Toast.LENGTH_LONG).show();
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View inputView = factory.inflate(R.layout.img_dialog, null);
ImageView imageView1 = (ImageView) findViewById(R.id.imgdata);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView1.setImageBitmap(bitmap);
// ダイアログの作成(AlertDialog.Builder)
final AlertDialog.Builder alt = new AlertDialog.Builder(MainActivity.this);
alt.setTitle("写真")
.setView(inputView)
.setPositiveButton("保存", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.setNegativeButton("キャンセル", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}});
final AlertDialog dialog = alt.create();
dialog.show();
} catch (Exception e) {
Log.e("era-", toString());
}
}
}
}
}