2012-12-12 64 views
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); 
} 
} 
+0

检查并确认它会在您的手机默认图像稠密度和您提供给相机意图的路径中创建重复图像。 –

+0

告诉我如何创建新文件夹?我dnr wana保存在phne默认画廊 –

+0

是您的代码无法生成名为'myfolder'的新文件夹? –

回答

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


} 

这完全适用于我。

查看此代码。

希望这可以帮助你。

谢谢。

+0

我这样做,并在文件夹中我的SD卡创建的Hello World BT图像不保存有关于手机图像gellery保存 保护无效startCameraActivity(){ \t Log.i( “MakeMachine”, “startCameraActivity()”) ; \t \t \t文件目录=新的文件(Environment.getExternalStorageDirectory()+文件分割符+ “的HelloWorld”); directory.mkdirs(); \t \t Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); \t intent.putExtra(MediaStore.EXTRA_OUTPUT,directory); \t \t startActivityForResult(intent,0); } –

+0

看看我发布的编辑解决方案。它运行良好,我可以看到创建的文件夹和图像也存储在该文件夹中。最后,我将在使用过的imageview中捕获图像。 –

+0

如果我有一个没有SD卡插槽的设备怎么办?我有一个视图,并希望创建一个文件夹并将图像保存在手机内存中。请帮助我 –

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. 
+0

context.getDir这一行显示错误上下文没有重新调整 –

+0

我在这段代码中写了什么然后 Uri outputFileUri = Uri.fromFile(file); –

+0

改为使用应用程序的上下文。 –

0

代码应该可以使用,但应该注意先创建这个目录。

相关问题