捕获图像并将其存储到新文件夹中的SD卡

时间:2013-01-24 12:37:36

标签: android android-sdcard android-camera-intent

我正在调用相机并捕获图像,我必须逐个捕获10张图像并将它们存储在SD卡上,然后才能将它们设置为图像视图。请检查我的下面的代码,它没有设置为图像视图。

如何将其存储在SD卡上并将其检索以设置为图像视图?如何在存储之前命名图像?

在我打电话给相机的第一项活动中:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mContext = this;
    init();
}

private void init() {
    String extStorageDirectory = Environment.getExternalStorageDirectory()
            + "/testing";

    File xmlDirectory = new File(extStorageDirectory);
    if (!xmlDirectory.exists())
        xmlDirectory.mkdirs();

    iv1 = (ImageView) findViewById(R.id.iv1);
}

private OnClickListener onBtnClicked = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case PHOTO:
                Intent selectImageIntent = new Intent(first.this,
                        second.class);
                startActivityForResult(selectImageIntent, 1);
                break;
        }
    }
};

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            String result = data.getStringExtra("result");

            Log.d("*****************",
                    "inside onactivityresult in main activity=" + result);

            Bitmap bitmap = BitmapFactory.decodeFile(result);
            iv1.setImageBitmap(bitmap);
            iv1.setScaleType(ScaleType.FIT_XY);
        }
    }
}

在我的第二个活动中,我正在捕捉图像并将其传递给第一个活动:

private void init() {
    picturePath = Environment.getExternalStorageDirectory() + "/Camera/"
            + "test.jpg";
    System.out.println("thumbnail path~~~~~~" + picturePath);
    File file = new File(picturePath);
    outputFileUri = Uri.fromFile(file);
}


public void startCamera() {
    Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(intent, IMAGE_CAPTURE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == IMAGE_CAPTURE) {
        if (resultCode == RESULT_OK) {
            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", picturePath);
            setResult(RESULT_OK, returnIntent);
            finish();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在Other Method中初始化对象不是一个好习惯。

iv1 = (ImageView) findViewById(R.id.iv1);方法移除此行init()并将您的OnCreate()更改为以下方式。

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
//Initialize here
    iv1 = (ImageView) findViewById(R.id.iv1);

    mContext = this;
    init();

}
希望它会对你有所帮助。

答案 1 :(得分:0)

使用此代码&把你的文件名和&路径,相机将捕捉图像&以给定名称存储它

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
           if (!APP_FILE_PATH_Media.exists()) 
          {
               APP_FILE_PATH_Media.mkdirs();
          }
         uriSavedImage =new File(APP_FILE_PATH_Media+ "/" +
                    "filename"+ ".jpeg");
       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(uriSavedImage));
       startActivityForResult(cameraIntent, CAMERA_REQUEST); 
onActivityResult()中的

使用此代码来设置imageView

try
                 {
                 BitmapFactory.Options options = new BitmapFactory.Options();
                 options.inSampleSize = 1;
                 bm = BitmapFactory.decodeFile(uriSavedImage.getAbsolutePath(), options);
                 }
                 catch(Exception ee)
                 {

                 }