2012-12-07 142 views
0

我有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; 
     } 
    } 
+0

當活動從SaveData返回時,您可以在AndroidCamera Activity的onActivityResult中更新您的EditText –

+0

您已經通過包bundle'putString(「path」,uriTarget.toString());'傳遞uri。因此,只需從SaveData Activity中的捆綁提取值。 –

+0

@ρяσѕρєяK如何做到這一點?你能給我一個樣本嗎?即時通訊新手在Android –

回答

0

這是你在AndroidCamera做過的事情:

Bundle bundle = new Bundle(); 
bundle.putString("path", uriTarget.toString()); 

Intent newIntent = new Intent(AndroidCamera.this, SaveData.class); 
newIntent.putExtras(bundle); 
startActivityForResult(newIntent, 0); 

改變這一行

startActivityForResult(newIntent, 0); 

startActivity(newIntent); 

現在,只需撥打這個代碼到你保存數據的活動:

UPDATE:

String imagePath=""; 
    if(getIntent().getExtras() != null){ 
     imagePath = getIntent().getExtras().getString("path"); 
     your_editText.setText(imagePath); 
    } 

希望這有助於你。

謝謝。

+0

我嘗試把'String imagePath = getIntent()。getExtras()。getString(「path」); your_edittext.setText(imagePath);'內部的OnCreate但錯誤nuLLpointerException行'String imagePath = getIntent()。getExtras()。getString(「path」);' –

+0

看到我編輯的答案這將解決您的問題。 –

+0

Edittext起作用,但saveData中的輸入消失了。 –

0

setContentView(R.layout.save);之前在之前寫下SaveData Activity的方法,它會解決你的問題。

EditText edt = (EditText) findViewById(R.id.txtStatus); 
+0

一個嘗試,但錯誤NPE行'String param1 = bundle.getString(「path」);'@Dipak Keshariya –

+0

@Alexbelek你會得到什麼錯誤? –

+0

logcat'12-07 16:49:54.632:ERROR/AndroidRuntime(5240):由於:java.lang.NullPointerException 12-07 16:49:54.632:ERROR/AndroidRuntime(5240):at com.app。 databasesample.SaveData.onCreate(SaveData.java:44) 12-07 16:49:54.632:ERROR/AndroidRuntime(5240):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) ' –

0

您應該調用方法「setResult」將結果提交給調用的Activity,然後在「onResume」中檢查結果。 但是給你一個一般的提示。在Android中,您可以利用Intent系統爲用戶帶來好處。一個例子是讓用戶選擇他的相機應用程序拍攝照片,而不是爲此編寫自己的活動。這爲您節省了大量時間,用戶很高興他可以使用適合他需求的特定相機應用程序,並且他追逐了數小時併爲其付費。

相關問題