2011-09-01 110 views
9

嘿,我是Android新手,最近我一直在嘗試製作應用程序。基本上,我想要做的是讓用戶創建自己的文件夾,然後轉到包含啓動相機按鈕的活動。從那裏我想能夠啓動相機並將相機圖像保存到新創建的文件夾中。我在將相機圖像保存到新創建的文件夾中的最後部分遇到問題。如何將圖像從Android攝像頭保存到特定文件夾?

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 


     EditText text = (EditText)findViewById(R.id.editText1); 
     EditText text2 = (EditText)findViewById(R.id.editText2); 



     @Override 
     public void onClick(View v) { 

      final String name = text.getText().toString(); 
      final String placeName = text2.getText().toString(); 

      String place = placeName.substring(0,3); 
      String direct = name + place ; 

      File folder = new File("/sdcard/CameraTest/" + direct + "/"); 
      folder.mkdirs(); 

      Intent myIntent = new Intent(CameraTestActivity.this, Press.class); 
      myIntent.putExtra("key", "/sdcard/CameraTest/" + direct + "/"); 
      startActivity(myIntent); 

     } 
    }); 

在這裏,我轉換到本次活動:

public class Press extends Activity { 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.afterenter); 
     final String direct = this.getIntent().getStringExtra("key"); 


     // TODO Auto-generated method stub 
     Button p = (Button) findViewById(R.id.button2); 
     p.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent camera= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

       startActivityForResult(camera, 1); 

      } 
     }); 



    Button np = (Button) findViewById(R.id.button3); 
    np.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent next = new Intent(Press.this, CameraTestActivity.class); 
      startActivity(next); 
     } 
    });   
    } 
} 

請告訴我如何從相機將圖像保存到新創建的文件夾。我希望用戶能夠拍攝多張照片,然後將這幾張照片保存到該特定文件夾中。提前致謝!

回答

11

呼叫相機活動之前添加此代碼,

Uri uriSavedImage=Uri.fromFile(new File("/sdcard/flashCropped.png")); 
camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
startActivityForResult(camera, 1); 
+0

什麼是「輸出「爲什麼要這樣工作?應該不使用某種常數嗎? –

+0

MediaStore.EXTRA_OUTPUT是常量 – Teodor

+2

不起作用。 EXTRA_OUTPUT有一些問題。 – zgulser

0

您應該將文件位置添加到圖像捕捉意圖。 例如:

camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, [file location]); 

看看here

1

試試這個....

路徑= Environment.getExternalStorageDirectory ()+「/photo1.jpg」;

 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, CAPTURE_IMAGE_ACTIVITY); 

,你還沒有實現的onActivityResult ....嘗試這個可以幫助ü

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    System.gc(); 
    if (requestCode == CAPTURE_IMAGE_ACTIVITY) { 
     if (resultCode == Activity.RESULT_OK) { 
      try { 
       // Call function MakeFolder to create folder structure if 
       // its not created 
       if(imageBitmap != null) { 
        imageBitmap = null; 
        imageBitmap.recycle(); 
       } 
       MakeFolder(); 
       // Get file from temp1 file where image has been stored 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 3; 
       imageBitmap = BitmapFactory.decodeFile(path, options); 
       imgForPhotograph.setImageBitmap(imageBitmap); 
       isImageTaken = true; 
       // Name for image 
       IMAGEPATH = getString(R.string.chassisImage) 
         + System.currentTimeMillis(); 
       SaveImageFile(imageBitmap,IMAGEPATH); 
      } catch (Exception e) { 
       Toast.makeText(this, "Picture Not taken", 
           Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } 
     } 
    } 

希望你得到它..........

+1

CAPTURE_IMAGE_ACTIVITY不過是一個整數,你可以使用簡單的整數值來表示你的選擇,比如說99/7/67。它用於檢查請求代碼。例如,1活動可以啓動許多其他活動,但每個請求都應該有唯一的請求代碼,可以在主活動中返回onActivityResult中的每個活動時進行檢查。檢查請求代碼以瞭解哪個活動已返回。 –

+0

@Richa什麼是isImageTaken什麼樣的isImageTaken變量.... – Amitsharma

相關問題