2 o casper ne O_Casper_ne 于 2013.03.25 09:57 提问

如何捕捉图像并保存在文件夹中

下面的代码是用于捕捉一个图像,并将它保存到手机图像目录。如何修改代码在内存中创建一个新的文件夹,然后再保存捕捉的图像,再在主屏中显示所有的图像。

package makemachine.android.examples;

import java.io.File;
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 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() + "/images 
  /make_machine_example.jpg";
}

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

1个回答

hxn_217
hxn_217   2013.03.25 13:48
已采纳

创建一个 ExternalStorage 中的特殊目录,把所有的文件保存到那个文件夹中。检索所有文件夹中的文件,并且在 Listview 或者 Gridview 中显示。如果你想显示很多图像,但要使用同一目录,最后用 currentTimeInMillis()生成文件名。

_path = Environment.getExternalStorageDirectory() + "/images 
  /make_machine_example.jpg";

也可以用下面的代码替代上面

_path = Environment.getExternalStorageDirectory() + "/images 
  /"+System.currentTimeMillis()+".jpg";

添加的代码来创建目录:

File dir = new File("path/to/your/directory");
try{
  if(dir.mkDir()) {
     System.out.println("Directory created");
  } else {
     System.out.println("Directory is not created");
}catch(Exception e){
  e.printStacktrace();
}

不要忘记添加权限:

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