如何将图片从Android的相机保存到特定的文件夹?

嗨,我是新来的Android,我一直在尝试最近做一个应用程序。 基本上,我想要做的是让用户做出自己的文件夹,然后去一个活动,其中包含一个button来启动相机。 从那里我想能够启动相机,并将相机图像保存到新创build的文件夹中。 我在将相机图像保存到新创build的文件夹的最后部分时遇到问题。

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { EditText text = (EditText)findViewById(R.id.editText1); EditText text2 = (EditText)findViewById(R.id.editText2); @Override public void onClick(View v) { final String name = text.getText().toString(); final String placeName = text2.getText().toString(); String place = placeName.substring(0,3); String direct = name + place ; File folder = new File("/sdcard/CameraTest/" + direct + "/"); folder.mkdirs(); Intent myIntent = new Intent(CameraTestActivity.this, Press.class); myIntent.putExtra("key", "/sdcard/CameraTest/" + direct + "/"); startActivity(myIntent); } }); 

从这里我转换到这个活动:

 public class Press extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.afterenter); final String direct = this.getIntent().getStringExtra("key"); // TODO Auto-generated method stub Button p = (Button) findViewById(R.id.button2); p.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent camera= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(camera, 1); } }); Button np = (Button) findViewById(R.id.button3); np.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent next = new Intent(Press.this, CameraTestActivity.class); startActivity(next); } }); } } 

请告诉我如何将相机中的图像保存到新创build的文件夹中。 我希望用户能够拍摄多张照片,然后将这几张照片保存到特定的文件夹中。 提前致谢!

Solutions Collecting From Web of "如何将图片从Android的相机保存到特定的文件夹?"

在调用相机活动之前添加此代码,

 Uri uriSavedImage=Uri.fromFile(new File("/sdcard/flashCropped.png")); camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); startActivityForResult(camera, 1); 

试试这个….

path = Environment.getExternalStorageDirectory()+“/photo1.jpg”;

  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, CAPTURE_IMAGE_ACTIVITY); 

而你还没有实现onActivityResult ….试试这可能会帮助你

 public void onActivityResult(int requestCode, int resultCode, Intent data) { System.gc(); if (requestCode == CAPTURE_IMAGE_ACTIVITY) { if (resultCode == Activity.RESULT_OK) { try { // Call function MakeFolder to create folder structure if // its not created if(imageBitmap != null) { imageBitmap = null; imageBitmap.recycle(); } MakeFolder(); // Get file from temp1 file where image has been stored BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3; imageBitmap = BitmapFactory.decodeFile(path, options); imgForPhotograph.setImageBitmap(imageBitmap); isImageTaken = true; // Name for image IMAGEPATH = getString(R.string.chassisImage) + System.currentTimeMillis(); SaveImageFile(imageBitmap,IMAGEPATH); } catch (Exception e) { Toast.makeText(this, "Picture Not taken", Toast.LENGTH_LONG).show(); e.printStackTrace(); } } } 

希望你得到它……….

您应该将文件位置添加到图像捕捉意图。 例如:

 camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, [file location]); 

看看这里