程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-如何捕捉圖像並保存在文件夾中

android-如何捕捉圖像並保存在文件夾中

編輯:編程綜合問答
如何捕捉圖像並保存在文件夾中

下面的代碼是用於捕捉一個圖像,並將它保存到手機圖像目錄。如何修改代碼在內存中創建一個新的文件夾,然後再保存捕捉的圖像,再在主屏中顯示所有的圖像。

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

最佳回答:


創建一個 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" />
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved