【问题标题】:Capture a image and store it to the SD Card in new folder捕获图像并将其存储到 SD 卡的新文件夹中
【发布时间】:2013-01-08 05:18:48
【问题描述】:

我正在调用相机并拍摄图像,我必须一张一张地拍摄 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();
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    在其他方法中初始化对象不是一个好习惯。

    init() 方法中删除这一行iv1 = (ImageView) findViewById(R.id.iv1); 并将您的OnCreate() 更改为Below Way。

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

    希望对你有所帮助。

    【讨论】:

    • @cavallo:你能把你的logcat贴在这里吗?
    • "在其他方法中初始化对象不是一个好习惯。" - 原因?想知道,因为我也在做同样的事情。
    【解决方案2】:

    使用此代码并输入您的文件名和路径,相机将捕获图像并以给定名称存储它

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多