如何将Android中Camera的图像保存到特定文件夹?

时间:2011-09-01 05:59:41

标签: android file camera save media

基本上,我想要做的是让用户自己制作folder,然后转到包含activity的{​​{1}}以启动button

从那里我希望能够启动camera并将camera图像保存到新创建的文件夹中。

我在将camera图像保存到新创建的文件夹中的最后一部分时遇到了问题。

这是我的camera

Code

从这里开始我转到这个活动:

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

        }
    });

请告诉我如何将<{1}}中的图像保存到新创建的文件夹中。

我希望用户能够拍摄多张图片,然后将这些图片保存到该特定文件夹中。

3 个答案:

答案 0 :(得分:13)

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

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

答案 1 :(得分: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();
            }
        }
    }

答案 2 :(得分:0)

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

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

看看here