2013-12-19 96 views
0

我希望我的應用能夠拍照(不是有意的)。 我想要我的活動A.class拍照並給予其他活動B.class我的圖片的absolutePath()在putExtra中打算調用B.Class。Android活動私人變量功能

這裏我的代碼

ANDROID CODE

public class A extends Activity { 
    private SurfaceHolder previewHolder=null; 
    private Camera camera=null; 
    private boolean inPreview=false; 
    private boolean cameraConfigured=false; 
    protected static final String TAG = "DEBUG_TAG"; 
    protected Throwable e; 
    private File pictureFile; 
    private String picturePath; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.capture); 
     SurfaceView preview=(SurfaceView)findViewById(R.id.SurfaceView_camera); 
     previewHolder=preview.getHolder(); 
     previewHolder.addCallback(surfaceCallback); 
     TextView capturer = (TextView)findViewById(R.id.TextView_capturer); 
     capturer.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       camera.takePicture(null, null, mPicture); 
(L.88)   call_next_activity(v); 
      } 
     }); 
    } 

    @Override 
    public void onPause() { 
     if (inPreview) { 
      camera.stopPreview(); 
     } 
     camera.release(); 
     camera=null; 
     inPreview=false; 
     super.onPause(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     camera=Camera.open(); 
     startPreview(); 
    } 

    private void initPreview(int width, int height) { 
     if (camera!=null && previewHolder.getSurface()!=null) { 
      try { 
       camera.setPreviewDisplay(previewHolder); 
      } catch (Throwable t) { 
       Log.e("PreviewDemo-surfaceCallback", "Exception in setPreviewDisplay()", t); 
       Toast.makeText(capture.this, t.getMessage(), Toast.LENGTH_LONG).show(); 
      } 

      if (!cameraConfigured) { 
       Camera.Parameters parameters=camera.getParameters(); 
       camera.setDisplayOrientation(90); 
       parameters.set("rotation", 90); 
       Camera.Size Previewsize=getBestPreviewSize(width, height, parameters); 
       parameters.setPreviewSize(Previewsize.width, Previewsize.height); 
       Camera.Size PictureSize = getBestPictureSize(width, height, parameters); 
       parameters.setPictureSize(PictureSize.width, PictureSize.height); 
       parameters.setJpegQuality(100); 
       camera.setParameters(parameters); 
       cameraConfigured=true; 
      } 
     } 
    } 

    private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) { 
     Camera.Size result = null; 
     for (Camera.Size size : parameters.getSupportedPreviewSizes()) { 
      if (size.width<=width && size.height<=height) { 
       if (result==null) { 
        result=size; 
       } else { 
        int resultArea=result.width*result.height; 
        int newArea=size.width*size.height; 
        if (newArea>resultArea) { 
         result=size; 
        } 
       } 
      } 
     } 
     return(result); 
    } 

    private Camera.Size getBestPictureSize(int width, int height, Camera.Parameters parameters) { 
     Camera.Size result = null; 
     for (Camera.Size size : parameters.getSupportedPictureSizes()) { 
      if (size.width>=width && size.height>=height) { 
       if (result==null) { 
        result=size; 
       } else { 
        int resultArea=result.width*result.height; 
        int newArea=size.width*size.height; 
        if (newArea>resultArea) { 
         result=size; 
        } 
       } 
      } 
     } 
     return(result); 
    } 

    private void startPreview() { 
     if (cameraConfigured && camera!=null) { 
      camera.startPreview(); 
      inPreview=true; 
     } 
    } 

    private static File getOutputMediaFile(){ 
     String NewFolder = "/TEST"; 
     String StorageDirectory; 
     StorageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); 
     File mediaStorageDir = new File(StorageDirectory + NewFolder); 
     if (!mediaStorageDir.exists()){ 
      if (!mediaStorageDir.mkdirs()){ 
       Log.d("Jours de Chefs", "failed to create directory"); 
       return null; 
      } else { 
       mediaStorageDir.mkdir(); 
      } 
     } 

     String date = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss", Locale.FRANCE).format(new Date()); 

     File photo = new File(StorageDirectory + NewFolder, "Full_TEST_" + date + ".jpg"); 
     return photo;  
    } 

    public void call_next_activity(View v) { 

     // ----- HERE I HAVE EXCEPTION -----// 
     // When i want to call picturePath or pictureFile, i get Exception : 
     // NullPointerException 
(L.246) if (!picturePath.isEmpty()) { 
      Toast.makeText(capture.this, "CA MARCHE !!!!", Toast.LENGTH_LONG).show(); 
     } else { 
      Toast.makeText(capture.this, "CA MARCHE PAS", Toast.LENGTH_LONG).show(); 
     } 
    } 

    private PictureCallback mPicture = new PictureCallback() { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      pictureFile = getOutputMediaFile(); 
      chemin_acces_photo = pictureFile.getPath(); 
      if (pictureFile == null){ 
       Log.d(TAG, "Error creating media file, check storage permissions: " + e.getMessage()); 
       return; 
      } 

      // ----- HERE TOAST SHOW ME RIGHT PATH ----- // 
      // NO EXCEPTION, NO BUG, EVERYTHING IS OKAY 
      Toast.makeText(capture.this, pictureFile.getAbsolutePath(), Toast.LENGTH_LONG).show();   

      try { 
       FileOutputStream fos = new FileOutputStream(pictureFile); 
       fos.write(data); 
       fos.close(); 
      } catch (FileNotFoundException e) { 
       Log.d(TAG, "File not found: " + e.getMessage()); 
      } catch (IOException e) { 
       Log.d(TAG, "Error accessing file: " + e.getMessage()); 
      } 
     } 
    }; 

    SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() { 
     public void surfaceCreated(SurfaceHolder holder) { 
      // no-op -- wait until surfaceChanged() 
     } 

     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
      initPreview(width, height); 
      startPreview(); 
     } 

     public void surfaceDestroyed(SurfaceHolder holder) { 
      // no-op 
     } 
    }; 
} 

一切工作的偉大,但是當我想打電話給pictureFile.getAbsolutePath()我有這樣的錯誤:

logcat的CODE

E/AndroidRuntime(14165): FATAL EXCEPTION: main 
E/AndroidRuntime(14165): java.lang.NullPointerException 
E/AndroidRuntime(14165): at XXXXXXXXXXXXXX.capture.call_next_activity(capture.java:246) 
E/AndroidRuntime(14165): at XXXXXXXXXXXXXX.capture$3.onClick(capture.java:88) 
E/AndroidRuntime(14165): at android.view.View.performClick(View.java) 
E/AndroidRuntime(14165): at android.view.View$PerformClick.run(View.java) 
E/AndroidRuntime(14165): at android.os.Handler.handleCallback(Handler.java) 
E/AndroidRuntime(14165): at android.os.Handler.dispatchMessage(Handler.java) 
E/AndroidRuntime(14165): at android.os.Looper.loop(Looper.java) 
E/AndroidRuntime(14165): at android.app.ActivityThread.main(ActivityThread.java) 
E/AndroidRuntime(14165): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(14165): at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime(14165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
E/AndroidRuntime(14165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
E/AndroidRuntime(14165): at dalvik.system.NativeStart.main(Native Method) 

有人可以解釋我爲什麼我的pictureFile == NULL?


編輯1: 梅爾基亞德斯回答

這是因爲camera.takePicture()是異步的。您有:

@Override 
public void onClick(View v) { 
    camera.takePicture(null, null, mPicture); 
    call_next_activity(v); //<----------here picturePath is not yet initialised 
} 

所以,當你調用takePicture()以上,它立即返回,call_next_activity()是>調用,但picturePath尚未設置。

將您的call_next_activity()代碼移到您的mPicture()回調函數中,設置AFTER圖片路徑。

尼斯解釋,但現在我已經得到了新的問題: 我call_next_activity(V)的代碼就是一個例子,在此功能,我想改變的活動,然後我真正的代碼,如果:

ANDROID CODE

public void call_next_activity(v) { 
    Intent intent = new Intent (this, next_activity.class); 
    intent.putExtra("image_path", picturePath); 
    startActivity(intent); 
} 

我不能把我的call_next_activity()代碼到mPicture回調,因爲我不會是我的觀點,但會Camera.Picture回撥(){}

任何想法?


編輯2ANSWER 關注梅爾基亞德斯編輯,只是刪除call_next_activity(v)的函數, 寫入call_next_activity代碼在onPictureTaken的端部()函數:

public void onPictureTaken(byte[] data, Camera camera) { 
    pictureFile = getOutputMediaFile(); 
    if (pictureFile == null){ 
     Log.d(TAG, "Error creating media file, check storage permissions: " + e.getMessage()); 
     return; 
    } 

    Toast.makeText(capture.this, pictureFile.getAbsolutePath(), Toast.LENGTH_LONG).show();   

    try { 
     FileOutputStream fos = new FileOutputStream(pictureFile); 
     fos.write(data); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.d(TAG, "File not found: " + e.getMessage()); 
    } catch (IOException e) { 
     Log.d(TAG, "Error accessing file: " + e.getMessage()); 
    } 

    // ----- call_next_activity code here ----- // 
    Intent intent = new Intent (getApplicationContext(), next_activity.class); 
    intent.putExtra("image_path", pictureFile.getAbsolutePath()); 
    startActivity(intent); 
} 

PS:我刪除了我的picturePath(string)變量,並通過pictureFile.getAbsolutePath()更改picturePath。

謝謝大家!

+0

你需要的上下文,所以要麼聲明私有上下文的背景下,並設置它的onCreate(),然後使用,而不是這一點,或使用getApplicationContext( )而不是這個。 – Melquiades

回答

1

這是因爲camera.takePicture()是異步的。您有:

@Override 
public void onClick(View v) { 
    camera.takePicture(null, null, mPicture); 
    call_next_activity(v); //<----------here picturePath is not yet initialised 
} 

所以,當你調用takePicture()以上,它立即返回,call_next_activity()被調用,但picturePath尚未設置。

將您的call_next_activity()代碼移到您的mPicture()回調函數中,設置AFTER圖片路徑。

編輯:

聲明

private Context context; 

在你的類,然後將其設置上的onCreate()的setContentView()後:

context = this; 

最後,用它來設置你的目的:

public void call_next_activity(v) { 
    Intent intent = new Intent (context, next_activity.class); 
    intent.putExtra("image_path", picturePath); 
    startActivity(intent); 
} 

或者,你可以使用:

Intent intent = new Intent(getApplicationContext(), next_activity.class); 

Intent intent = new Intent(A.this, next_activity.class); 
+0

你能看看我的編輯1嗎?謝謝 – locobastos

0

根據你的代碼有問題,我想你會得到java.lang.NullPointerException,因爲你的String變量picturePath沒有任何價值。請給它初始化一些價值。

希望它會幫助你

+0

我認爲Melquiades的答案更好......我不想用「foo」初始化我的變量。我想我的變量== null在開始處... 順便說一句,感謝您的回答! – locobastos

+0

如果您的問題通過使用他的方法解決,我沒有任何問題。 @locobastos – Andrain