2012-12-12 60 views
1

如何在手機內存中創建新文件夾「MyFolder」並將所有捕獲的圖像保存在該文件夾中? 請幫幫我。以下代碼將所有圖像保存到手機的默認圖像庫。我想創建新文件夾並將所有捕獲的圖像保存在該文件夾中。我該怎麼辦?如何在手機內存中創建文件夾並保存圖像。

public class PhotoCaptureExample extends Activity 
    { 
protected Button _button; 
protected ImageView _image; 
protected TextView _field; 
protected String _path; 
protected boolean _taken; 

protected static final String PHOTO_TAKEN = "photo_taken"; 

@Override 
    public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    _image = (ImageView) findViewById(R.id.image); 
    _field = (TextView) findViewById(R.id.field); 
    _button = (Button) findViewById(R.id.button); 
    _button.setOnClickListener(new ButtonClickHandler()); 
    ; 
    _path = Environment.getExternalStorageDirectory() + "/myfolder 
/"+System.currentTimeMillis()+".jpg"; 




    File dir = new File(_path); 
    try{ 
     if(dir.mkdir()) { 
     System.out.println("Directory created"); 
     } 
     else { 
     System.out.println("Directory is not created"); 
     }} 
     catch(Exception e){ 

    } 



















} 

public class ButtonClickHandler implements View.OnClickListener 
{ 
    public void onClick(View view){ 
     Log.i("MakeMachine", "ButtonClickHandler.onClick()"); 
     startCameraActivity(); 
    } 
} 

protected void startCameraActivity() 
{ 
    Log.i("MakeMachine", "startCameraActivity()"); 
    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, 0); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    Log.i("MakeMachine", "resultCode: " + resultCode); 
    switch(resultCode) 
    { 
     case 0: 
      Log.i("MakeMachine", "User cancelled"); 
      break; 

     case -1: 
      onPhotoTaken(); 
      break; 
    } 
} 

protected void onPhotoTaken() 
{ 
    Log.i("MakeMachine", "onPhotoTaken"); 

    _taken = true; 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 4; 

    Bitmap bitmap = BitmapFactory.decodeFile(_path, options); 

    _image.setImageBitmap(bitmap); 

    _field.setVisibility(View.GONE); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState){ 
    Log.i("MakeMachine", "onRestoreInstanceState()"); 
    if(savedInstanceState.getBoolean(PhotoCaptureExample.PHOTO_TAKEN)) { 
     onPhotoTaken(); 
    } 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    outState.putBoolean(PhotoCaptureExample.PHOTO_TAKEN, _taken); 
} 
} 
+0

檢查並確認它會在您的手機默認圖像稠密度和您提供給相機意圖的路徑中創建重複圖像。 –

+0

告訴我如何創建新文件夾?我dnr wana保存在phne默認畫廊 –

+0

是您的代碼無法生成名爲'myfolder'的新文件夾? –

回答

0

您需要下列權限在您的清單文件中創建文件夾。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 

更新:

package com.test; 

import java.io.File; 

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.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 


public class CameraTest extends Activity 
{ 
    protected Button _button; 
    protected ImageView _image; 
    protected TextView _field; 
    protected String _path; 
    protected boolean _taken; 

protected static final String PHOTO_TAKEN = "photo_taken"; 

@Override 
    public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.camtest); 

    _image = (ImageView) findViewById(R.id.image); 
    _field = (TextView) findViewById(R.id.field); 
    _button = (Button) findViewById(R.id.button); 
    _button.setOnClickListener(new ButtonClickHandler()); 
    // _path = Environment.getExternalStorageDirectory() + "/NewFolder/"+System.currentTimeMillis()+".jpg"; 

    _path = "/sdcard/NewFolder/test1.jpg"; 


    String dir = "/sdcard/NewFolder/"; 
    File imageDirectory = new File(dir); 
    imageDirectory.mkdirs(); 

    /* File dir = new File(_path); 
    try{ 
     if(dir.mkdir()) { 
     System.out.println("Directory created"); 
     } 
     else { 
     System.out.println("Directory is not created"); 
     }} 
     catch(Exception e){ 

    } */ 
} 

public class ButtonClickHandler implements View.OnClickListener 
{ 
    public void onClick(View view){ 
     Log.i("MakeMachine", "ButtonClickHandler.onClick()"); 
     startCameraActivity(); 
    } 
} 

protected void startCameraActivity() 
{ 
    Log.i("MakeMachine", "startCameraActivity()"); 
    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, 0); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    Log.i("MakeMachine", "resultCode: " + resultCode); 
    switch(resultCode) 
    { 
     case 0: 
      Log.i("MakeMachine", "User cancelled"); 
      break; 

     case -1: 
      onPhotoTaken(); 
      break; 
    } 
} 

protected void onPhotoTaken() 
{ 
    Log.i("MakeMachine", "onPhotoTaken"); 

    _taken = true; 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 4; 

    Bitmap bitmap = BitmapFactory.decodeFile(_path, options); 

    _image.setImageBitmap(bitmap); 

    _field.setVisibility(View.GONE); 
} 


} 

這完全適用於我。

查看此代碼。

希望這可以幫助你。

謝謝。

+0

我這樣做,並在文件夾中我的SD卡創建的Hello World BT圖像不保存有關於手機圖像gellery保存 保護無效startCameraActivity(){ \t Log.i( 「MakeMachine」, 「startCameraActivity()」) ; \t \t \t文件目錄=新的文件(Environment.getExternalStorageDirectory()+文件分割符+ 「的HelloWorld」); directory.mkdirs(); \t \t Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); \t intent.putExtra(MediaStore.EXTRA_OUTPUT,directory); \t \t startActivityForResult(intent,0); } –

+0

看看我發佈的編輯解決方案。它運行良好,我可以看到創建的文件夾和圖像也存儲在該文件夾中。最後,我將在使用過的imageview中捕獲圖像。 –

+0

如果我有一個沒有SD卡插槽的設備怎麼辦?我有一個視圖,並希望創建一個文件夾並將圖像保存在手機內存中。請幫助我 –

0

檢查,這可能幫助,

File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir; 
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir. 
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file. 
+0

context.getDir這一行顯示錯誤上下文沒有重新調整 –

+0

我在這段代碼中寫了什麼然後 Uri outputFileUri = Uri.fromFile(file); –

+0

改爲使用應用程序的上下文。 –

0

代碼應該可以使用,但應該注意先創建這個目錄。

相關問題