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

3 に答える 3

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


}

これは私にとって完璧に機能します。

このコードでチェックしてください。

これがお役に立てば幸いです。

ありがとう。

于 2012-12-12T07:13:50.613 に答える
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.
于 2012-12-12T06:31:12.393 に答える
-1

コードは使用できますが、最初にこのディレクトリを作成するように注意する必要があります。

于 2012-12-12T09:37:32.827 に答える