2 eiseno Eiseno 于 2012.12.12 14:50 提问

怎么在电话记忆卡里创建文件夹然后保存图像?

怎么在电话的记忆卡创建新的文件夹"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 );
}
}

4个回答

fire_fire_fire
fire_fire_fire   Rxr 2012.12.13 10:34
已采纳

参考,这个应该能够满足你的要求:Android调用手机拍照,将拍照的图片放在指定文件夹中

cytown
cytown   2012.12.12 16:55

你那个代码应该就可以用,不过要注意要先创建这个目录就行。

yijidemomo
yijidemomo   2012.12.13 10:42

看下这个,可能对你有帮助

File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //创建一个内部dir;
File fileWithinMyDir = new File(mydir, "myfile"); //在内部dir得到一个文件夹.
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
yijidemomo
yijidemomo 用应用的context
5 年多之前 回复
Eiseno
Eiseno context.getDir这一行显示错误context not reconize
5 年多之前 回复
Sueyexin
Sueyexin   2012.12.13 11:12

如果你要创建文件夹,你应该在你的manifest文件中加下边的这个许可。

<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 );
}


}

这个在我这是好的,你看一下,希望对你有帮助。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!