2013-01-16 53 views
0

我想創建我自己的相機應用程序。主要活動有一個按鈕,點擊後,它將進入相機活動(帶有預覽和「拍攝」按鈕)。一旦我按下「拍」按鈕,圖片將被拍攝。拍攝的照片將返回到主要活動並顯示在圖像視圖上。 問題是,我不知道如何將照片從相機活動傳回主要活動。希望任何人都可以提供一個提示或例子?從相機活動獲取照片並顯示主要活動,android

+0

你谷歌還不夠嗎? –

回答

0

發送意向到相機屏幕的路徑。該圖像將被捕獲,存儲在該路徑上的&。

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      if (!APP_FILE_PATH_Media.exists()) 
      { 
       APP_FILE_PATH_Media.mkdirs(); 
      } 
     uriSavedImage =new File(APP_FILE_PATH_Media+ "/" + 
        "IMG_"+ getTimeStamp() + ".jpeg"); 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(uriSavedImage)); 
     startActivityForResult(cameraIntent, CAMERA_REQUEST); 

現在,當你點擊它將調用你的主Activity的ActivityResult方法。 在OnActivityResult:

path=uriSavedImage.getAbsolutePath(); 

您可以使用此路徑用於顯示圖像。

0

確實!你必須發送相機意圖與startActivityForResult,有整理以下的代碼來看看:

定義要存儲圖像路徑:

String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+filename; 
File file = new File(_path); 
Uri outputFileUri = Uri.fromFile(file); 

相機意向

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
      startActivityForResult(intent, 1212); 

覆蓋startActivityForResult

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

      if (requestCode == 1212) { 
      //here you can decode your path and use for displaying an image. 
     } 
    } 
+0

但我不想使用內置的相機應用程序。我如何打電話給我自己的相機活動(使用LiveView /預覽和「拍」按鈕) 如何在傳遞意圖時打電話給我自己的相機活動? – user1983585

0

嗨,請嘗試使用此代碼。

package com.example.stackoverflow; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.math.BigInteger; 
import java.security.SecureRandom; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.KeyEvent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class MyCameraActivity extends Activity { 
    private static final int CAMERA_REQUEST = 1888; 



    static String str_Camera_Photo_ImagePath = ""; 
    private static File f; 
    private static int Take_Photo = 2; 
    private static String str_randomnumber = ""; 
    static String str_Camera_Photo_ImageName = ""; 
    public static String str_SaveFolderName; 
    private static File wallpaperDirectory; 
    Bitmap bitmap; 
    int storeposition = 0; 
    public static GridView gridview; 
    public static ImageView imageView; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ccccc); 
     this.imageView = (ImageView)this.findViewById(R.id.imageView1); 
     Button photoButton = (Button) this.findViewById(R.id.button1); 
     photoButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       str_SaveFolderName = Environment 
         .getExternalStorageDirectory() 
         .getAbsolutePath() 
         + "/rajeshsample"; 
       str_randomnumber = String.valueOf(nextSessionId()); 
       wallpaperDirectory = new File(str_SaveFolderName); 
       if (!wallpaperDirectory.exists()) 
        wallpaperDirectory.mkdirs(); 
       str_Camera_Photo_ImageName = str_randomnumber 
         + ".jpg"; 
       str_Camera_Photo_ImagePath = str_SaveFolderName 
         + "/" + str_randomnumber + ".jpg"; 
       System.err.println(" str_Camera_Photo_ImagePath " 
         + str_Camera_Photo_ImagePath); 
       f = new File(str_Camera_Photo_ImagePath); 
       startActivityForResult(new Intent(
         MediaStore.ACTION_IMAGE_CAPTURE).putExtra(
         MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)), 
         Take_Photo); 
       System.err.println("f " + f); 
      } 
     }); 
    } 


    // used to create randon numbers 
    public String nextSessionId() { 
     SecureRandom random = new SecureRandom(); 
     return new BigInteger(130, random).toString(32); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     // TODO Auto-generated method stub 
     if (event.getAction() == KeyEvent.ACTION_DOWN) { 
      switch (keyCode) { 
      case KeyEvent.KEYCODE_HOME: 
       finish(); 
       Toast.makeText(getApplicationContext(), "TTTTTTTTTTTT", Toast.LENGTH_LONG).show(); 
       return true; 
      } 
     } 

     return super.onKeyDown(keyCode, event); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == Take_Photo) { 
      String filePath = null; 

      filePath = str_Camera_Photo_ImagePath; 
      if (filePath != null) { 
       Bitmap faceView = (new_decode(new File(
           filePath))); // ========================> good 
               // lines 



       imageView.setImageBitmap(faceView); 

      } else { 
       bitmap = null; 
      } 
     } 
    } 

    public static Bitmap new_decode(File f) { 

     // decode image size 

     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     o.inDither = false; // Disable Dithering mode 

     o.inPurgeable = true; // Tell to gc that whether it needs free memory, 
           // the Bitmap can be cleared 

     o.inInputShareable = true; // Which kind of reference will be used to 
            // recover the Bitmap data after being 
            // clear, when it will be used in the future 
     try { 
      BitmapFactory.decodeStream(new FileInputStream(f), null, o); 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 300; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/1.5 < REQUIRED_SIZE && height_tmp/1.5 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 1.5; 
      height_tmp /= 1.5; 
      scale *= 1.5; 
     } 

     // decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     // o2.inSampleSize=scale; 
     o.inDither = false; // Disable Dithering mode 

     o.inPurgeable = true; // Tell to gc that whether it needs free memory, 
           // the Bitmap can be cleared 

     o.inInputShareable = true; // Which kind of reference will be used to 
            // recover the Bitmap data after being 
            // clear, when it will be used in the future 
     // return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     try { 

//   return BitmapFactory.decodeStream(new FileInputStream(f), null, 
//     null); 
      Bitmap bitmap= BitmapFactory.decodeStream(new FileInputStream(f), null, null); 
      System.out.println(" IW " + width_tmp); 
      System.out.println("IHH " + height_tmp);   
       int iW = width_tmp; 
       int iH = height_tmp; 

       return Bitmap.createScaledBitmap(bitmap, iW, iH, true); 

     } catch (OutOfMemoryError e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
      // clearCache(); 

      // System.out.println("bitmap creating success"); 
      System.gc(); 
      return null; 
      // System.runFinalization(); 
      // Runtime.getRuntime().gc(); 
      // System.gc(); 
      // decodeFile(f); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return null; 
     } 

    } 

} 
0

使用按鈕單擊事件

Intent action = new Intent("android.media.action.IMAGE_CAPTURE"); 
action.putExtra(MediaStore.EXTRA_OUTPUT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
startActivityForResult(action,TAKE_PICTURE_FROM_CAMERA); 

下面的代碼,你必須使用

public void onActivityResult(int reqCode, int resultCode, Intent data) { 
    super.onActivityResult(reqCode, resultCode, data); 

} 

if (resultCode == Activity.RESULT_OK) { 
       uri = data.getData(); 
       try { 
        photoBitMap = (Bitmap) data.getExtras().get("data"); 
        int h = 100; // height in pixels 
        int w = 100; // width in pixels 
        photoBitMap = Bitmap.createScaledBitmap(photoBitMap, h, w, 
          true); 

        uri = Uri.parse(android.provider.MediaStore.Images.Media 
          .insertImage(getContentResolver(), photoBitMap, 
            null, null)); 
        Bitmap usableBMP = Bitmap.createScaledBitmap(photoBitMap, 
          68, 80, true); 

        if (!Calculations.encodeImageToBase64(photoBitMap).equals(
          "") 
          && Calculations.encodeImageToBase64(photoBitMap) != null) { 
         // userPhoto.setImageBitmap(usableBMP); 
         userPhoto.setImageBitmap(Calculations 
           .getRoundedCornerBitmap(this, usableBMP)); 

        } else { 
         userPhoto.setImageBitmap(null); 
         userPhoto.setImageResource(R.drawable.default_user_img); 
         Toast.makeText(RegisterActivity.this, 
           getString(R.string.imageSizeValidation), 
           Toast.LENGTH_SHORT).show(); 
        } 
        userPhoto.setScaleType(ScaleType.FIT_XY); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } catch (OutOfMemoryError e) { 
        e.printStackTrace(); 
        Toast.makeText(RegisterActivity.this, 
          getString(R.string.imageSizeValidation), 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 
相關問題