通过在android中创建将相机图像存储在特定文件夹中

时间:2016-03-01 11:58:42

标签: java android image storage

imgFavorite.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
            open();
         }
      });
   }
   public void open(){
      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

      startActivityForResult(intent, 0);
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
      super.onActivityResult(requestCode, resultCode, data);
      Bitmap bp = (Bitmap) data.getExtras().get("data");

      imgFavorite.setImageBitmap(bp);
     Uri selectedImageUri = data.getData();
   String selectedImagePath = getRealPathFromURI(selectedImageUri);// this is the location /storage/emulated/0/DCIM/100ANDRO 
});

单击按钮imgFavorite后,它会在拍摄照片后打开默认相机,并将其存储在名为/storage/emulated/0/DCIM/100ANDRO的默认位置。  但是我必须通过在android代码中创建将捕获图像存储在特定文件夹中。我尝试了很多例子,但它不起作用。 任何人都可以告诉我代码的片段,以创建一个文件夹并存储捕获图像。

3 个答案:

答案 0 :(得分:0)

打开相机时,您可以指定存储相机所点击图像的路径。看看

imgFavorite.setOnClickListener(new OnClickListener()  {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                            String imagePath = "filename" + ".jpg";
                            File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Directory_Name" + File.separator+ "" + Constants.ORN);
                            root.mkdirs();
                            if (!root.exists()) {
                                root.mkdir();
                            }

                            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Directory_Name" + File.separator + "" + Constants.ORN, imagePath);
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            Uri outputFileUri = Uri.fromFile(file);
                            mImageCaptureUri = outputFileUri;
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                            startActivityForResult(intent, REQUEST_CODE);

                    }
                });

希望它适合你。

答案 1 :(得分:0)

distributionUrl=\
  https\://services.gradle.org/distributions/gradle-X.X.X-all.zip

答案 2 :(得分:-1)

试试这段代码。

imgFavorite.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
        open();
     }
      });
   }
public void open(){
      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getOutputMediaFile()));//
    startActivityForResult(intent, REQUEST_CODE);
   }

private File getOutputMediaFile() {
    // make a new file directory inside the "sdcard" folder
    File mediaStorageDir = new File(
            Environment.getExternalStorageDirectory(), "/myApp");

    // if this "Camera folder does not exist
    if (!mediaStorageDir.exists()) {
        // if you cannot make this folder return
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    // take the current timeStamp
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date(System.currentTimeMillis()));
    File mediaFile;
    // and make a media file:
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");
    currentFilePath = mediaFile.getAbsolutePath();
    return mediaFile;
}