2011-07-28 13 views
1

私はシステムカメラを呼び出して、システムビューでたくさんの写真を撮りたいですし、それらの写真をいくつかのフォルダに保存したいと思います。アンドロイドシステムカメラに電話して、画像をフォルダに保存する方法は?

  Intent imageCaptureIntent = new Intent("android.media.action.STILL_IMAGE_CAMERA"); 
     File out = new File(Environment.getExternalStorageDirectory(), "camera"+System.currentTimeMillis()+".jpg"); 
     Uri uri = Uri.fromFile(out); 
     imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
     imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 
     startActivityForResult(imageCaptureIntent, 1); 

このコードは私がシステム・ビューの写真をたくさん取ることができますが、それは私がしたいフォルダ(Environment.getExternalStorageDirectory()+「カメラ」を格納することはできません。私は、以下のようないくつかのコードを検索してい+ System.currentTimeMillis()+ "。jpg")。だから、誰かが自分の写真を自分が望むフォルダに保存する良い方法を持っている。あなたのonCreateで

答えて

3

()これを行う、

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    startActivityForResult(cameraIntent, TAKE_PICTURE_WITH_CAMERA); 

そしてonActivityResultで

、それはデフォルトのフォルダ

に格納フォルダを作成していない上記のコードで

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 

     if (resultCode != RESULT_OK) 
      return; 

     try { 
     AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r"); 
     FileInputStream fis = videoAsset.createInputStream(); 
     File tmpFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); 
     FileOutputStream fos = new FileOutputStream(tmpFile); 

     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = fis.read(buf)) > 0) { 
      fos.write(buf, 0, len); 
     }  
     fis.close(); 
     fos.close(); 
     } catch (IOException io_e) { 
     // TODO: handle error 
     } 
} 
+0

ええ、それは良い方法です!ただし、このビューを終了するにはどうすればよいですか? – Dalen

+0

シンプルなバックプレスイベント私は信じています –

1

が、このコードはストアです特定のフォルダ内の画像

public class MainActivity extends ActionBarActivity { 

     Button btnImage; 
     ImageView imageView; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      imageView= (ImageView) findViewById(R.id.imageView); 
      btnImage= (Button) findViewById(R.id.buttonCapture); 
      btnImage.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(cameraIntent, 0); 
       } 
      }); 

     } 
     protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
      super.onActivityResult(requestCode, resultCode, intent); 

      if (resultCode != RESULT_OK) 
       return; 

      try { 
       AssetFileDescriptor imageAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r"); 
       FileInputStream fis = imageAsset.createInputStream(); 

       File root=new File(Environment.getExternalStorageDirectory(),"/Imagesss/");//Folder Name Imagesss 
       if (!root.exists()) { 
        System.out.println("No directory"); 
        root.mkdirs(); 
       } 

       File file; 
       file=new File(root,"android_"+System.currentTimeMillis()+".jpg"); 

       FileOutputStream fos = new FileOutputStream(file); 

       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = fis.read(buf)) > 0) { 
        fos.write(buf, 0, len); 
       } 
       fis.close(); 
       fos.close(); 


      } catch (IOException io_e) { 
       // TODO: handle error 
      } 

      //Display Data In Image View 
      if (requestCode == 0 && resultCode == RESULT_OK) { 
       Bundle extras = intent.getExtras(); 
       Bitmap imageBitmap = (Bitmap) extras.get("data"); 
       imageView.setImageBitmap(imageBitmap); 
      } 
     } 
関連する問題