我有2項活動。 activity saveData和activity androidCamera攝像頭拍照後更新EditText
首先,我在saveData中有一個表單。當表單已被輸入,除了EditText「Upload Photo」。然後,我點擊按鈕相機採取uri /路徑圖像。當finish()
攝像機活動發送值uri/path返回到SaveData窗體Activity。問題是如何更新SaveData中的EditText「上傳照片」,所以當我拍照時,相機發送路徑/ URI裏面的EditText「上傳照片」?
這是我的活動AndroidCamera
package com.app.databasesample;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.Toast;
public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
final int RESULT_SAVEIMAGE = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_camera);
//
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
}});
}
ShutterCallback myShutterCallback = new ShutterCallback(){
@Override
public void onShutter() {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_RAW = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
/*Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */
Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
"Image Tersimpan: " + uriTarget.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sFilename="image";
Bundle bundle = new Bundle();
bundle.putString("path", uriTarget.toString());
Intent newIntent = new Intent(AndroidCamera.this, SaveData.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
//
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
這是我的活動保存數據
package com.app.databasesample;
import java.io.File;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SaveData extends Activity implements OnClickListener {
private DataManipulator dh;
static final int DIALOG_ID = 0;
private static final boolean TEST_ONLY_NO_FOTO=false;
private static final boolean TAKE_FOTO_HIRES=false;
private Uri outputFileUri;
File outputFile;
private String sDirectory="";
//String sFilename="";
public static final int SHOW_SUB_ACTIVITY_TAKE_PICTURE=5;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.save);
View add = findViewById(R.id.Button01add);
add.setOnClickListener(this);
View home = findViewById(R.id.Button01home);
home.setOnClickListener(this);
View kamera = findViewById(R.id.kamera);
kamera.setOnClickListener(this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (SHOW_SUB_ACTIVITY_TAKE_PICTURE) : {
if (resultCode == Activity.RESULT_OK) {
String newText = data.getStringExtra("image");
// TODO Update your TextView.
//EditText edt = (newText) findViewById(R.id.txtStatus);
//edt.setText(uriTarget.toString());
EditText edt = (EditText) findViewById(R.id.txtStatus);
edt.setText(newText);
}
break;
}
}
}
public void onClick(View v){
switch(v.getId()){
case R.id.Button01home:
Intent i = new Intent(this, DatabaseSample.class);
startActivity(i);
break;
case R.id.Button01add:
View editText1 = (EditText) findViewById(R.id.idHandheld);
View editText2 = (EditText) findViewById(R.id.namaHama);
View editText3 = (EditText) findViewById(R.id.jumlahHama);
String myEditText1=((TextView) editText1).getText().toString();
String myEditText2=((TextView) editText2).getText().toString();
String myEditText3=((TextView) editText3).getText().toString();
this.dh = new DataManipulator(this);
this.dh.insert(myEditText1,myEditText2,myEditText3);
showDialog(DIALOG_ID);
break;
case R.id.kamera:
Intent i1 = new Intent(this, AndroidCamera.class);
startActivityForResult(i1,SHOW_SUB_ACTIVITY_TAKE_PICTURE);
break;
}
}
private void SaveDatas(){
Date dt=new Date();
View editText4 = (EditText) findViewById(R.id.idHandheld);
View editText5 = (EditText) findViewById(R.id.namaHama);
View editText6 = (EditText) findViewById(R.id.jumlahHama);
String myEditText4=((TextView) editText4).getText().toString();
String myEditText5=((TextView) editText5).getText().toString();
String myEditText6=((TextView) editText6).getText().toString();
this.dh = new DataManipulator(this);
this.dh.insert(myEditText4,myEditText5,myEditText6);
showDialog(DIALOG_ID);
}//SaveData
protected final Dialog onCreateDialog(final int id) {
Dialog dialog = null;
switch(id){
case DIALOG_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("input data hama sukses disave | Tambah Input ?").setCancelable(false).setPositiveButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
SaveData.this.finish();
}
}).setNegativeButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
dialog = alert;
break;
default:
}
return dialog;
}
}
當活動從SaveData返回時,您可以在AndroidCamera Activity的onActivityResult中更新您的EditText –
您已經通過包bundle'putString(「path」,uriTarget.toString());'傳遞uri。因此,只需從SaveData Activity中的捆綁提取值。 –
@ρяσѕρєяK如何做到這一點?你能給我一個樣本嗎?即時通訊新手在Android –