创建文件夹并通过相机存储图像

时间:2015-02-28 10:28:31

标签: java android camera

请阅读我的代码问解决方案如何创建文件夹

公共类CameraActivity扩展了ActionBarActivity {

private static final int ACTION_TAKE_IMAGE = 1;

Button btnImage;
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    imageView = (ImageView)findViewById(R.id.imageView);

    btnImage= (Button) findViewById(R.id.buttonCapture);
    btnImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent takeVideoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takeVideoIntent, ACTION_TAKE_IMAGE);
        }
    });
}

@onActivityResult start

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK)
        {
            try
            {
                AssetFileDescriptor imageAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
                FileInputStream fis = imageAsset.createInputStream();
                File root=new File(Environment.getExternalStorageDirectory(),"/Vipul/RecordImage/");
                if (!root.exists()) {
                    System.out.println("No directory");
                    root.mkdirs();
                }

                File file;
                file=new File(root,"IMG_"+System.currentTimeMillis()+".jpg" );

                FileOutputStream fos = new FileOutputStream(file);

                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    fos.write(buf, 0, len);
                }
                fis.close();
                fos.close();

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        if(requestCode == ACTION_TAKE_IMAGE && resultCode == RESULT_OK){
            Bundle bundle = data.getExtras();
            Bitmap imageBitmap = (Bitmap) bundle.get("data");
            imageView.setImageBitmap(imageBitmap);
        }
    }
}

此代码工作2.3捕获图像但工作而不是创建文件夹不存储在我的特定文件夹中请询问您的答案

3 个答案:

答案 0 :(得分:2)

<强>更新

//declare

     int num = 0;

//.....//


//*onclik button

Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        //Create folder
        File imagesFolder = new File(
                Environment.getExternalStorageDirectory(), "Myfolder");
        imagesFolder.mkdirs();

        //Asign name for image
        File image = new File(imagesFolder, "Pic" + (num +1) + ".png");
        Uri uriSavedImage = Uri.fromFile(image);

        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

        //launch camera app with result code (forResult)
        startActivityForResult(cameraIntent, 1);


protected void onActivityResult (int requestCode, int resultCode, Intent data)
    {

            if (requestCode == 1 && resultCode == RESULT_OK)
            {
                //create bitmap with image

                Bitmap bMap = BitmapFactory.decodeFile(
                        Environment.getExternalStorageDirectory() +
                                "/Myfolder/" + "Pic" + num + ".png");
                //Add bitmap to ImageView
                //Show on screen
                imageView.setImageBitmap(bMap);
 }

答案 1 :(得分:1)

每次使用方法getTimeInMillis()为Calendar mCalendar = Calendar.getInstance(); String n = ""+mCalendar.getTimeInMillis(); String fname = "Pic-" + n.substring(5) + ".png"; //Asign name for image File image = new File(imagesFolder, fname); Uri uriSavedImage = Uri.fromFile(image);

时,要为图像使用不同的名称

答案 2 :(得分:1)

试试这段代码

public class CameraActivity extends ActionBarActivity {

//    private static final int ACTION_TAKE_IMAGE = 1;

    Button btnImage;
    ImageView imageView;
    File image;
    String picturePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        imageView = (ImageView) findViewById(R.id.imageView);

        btnImage = (Button) findViewById(R.id.buttonCapture);
        btnImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                //Create folder
                File imagesFolder = new File(
                        Environment.getExternalStorageDirectory(), "Vipul/image");
                imagesFolder.mkdirs();

                //Asign name for image
                String fname = "Pic-" + System.currentTimeMillis() + ".png";
                image= new File(imagesFolder, fname);

                Uri uriSavedImage = Uri.fromFile(image);

                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

                //launch camera app with result code (forResult)
                startActivityForResult(cameraIntent, 1);

            }

        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1 && resultCode == RESULT_OK) {
            Bitmap bitmap;

            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

            picturePath=image.getAbsolutePath();

            bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bitmapOptions);

            imageView.setImageBitmap(bitmap);
        }
    }
}

最终解决方案