2

这是我的代码,它获取图像并将其保存在手机图像目录中。如何修改此代码以在内存中创建一个新文件夹并将捕获的图像保存在那里,并在主屏幕中显示所有图像。我按照这个指南

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

2 回答 2

3

此代码正在运行

public class MainActivity extends ActionBarActivity {

    protected Button _button;
    protected ImageView _image;
    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.activity_main);

        _image = ( ImageView ) findViewById( R.id.imageView );
        _button = ( Button ) findViewById( R.id.buttonCapture );
        _button.setOnClickListener((android.view.View.OnClickListener) new ButtonClickHandler());

        _path = Environment.getExternalStorageDirectory() + "/MyFolder/"+System.currentTimeMillis()+".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);


    }

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

    @Override
    protected void onSaveInstanceState( Bundle outState ) {
        outState.putBoolean( MainActivity.PHOTO_TAKEN, _taken );
    }
}
于 2015-02-27T06:44:43.910 回答
1

嘿,根据需要在 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" />
于 2012-12-11T12:51:13.243 回答