android - 如何将图像存储在自定义文件夹中?

时间:2016-10-24 15:54:39

标签: android image directory

我想把它们存放在一个单独的文件夹中。拍摄图像已经完成,但我只能存储它们的sdcard根文件夹。 Environment.getExternalStorageDirectory().getAbsolutePath()给了我 /storage/emulated/0路径。我想要的是创建另一个名为edit.getText().toString()的文件夹,提前致谢。

private void startCapture() {

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (cameraIntent.resolveActivity(getPackageManager()) != null) {

        File photoFile = null;
        try {
            photoFile = CreateImageFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(photoFile != null)
        {
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(cameraIntent, CAMERA_CAPTURE);
        }
    }
}

private File CreateImageFile() throws IOException
{
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = timeStamp + ".jpg";


    File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+edit.getText().toString());
    if (!f.exists()) {
        f.mkdirs();
    } else {
        f = new File(Environment.getExternalStorageDirectory(), edit.getText().toString() + "(2)");
        f.mkdirs();
    }

    File storageDirectory = new File(new File("/sdcard/"+edit.getText().toString),imageFileName);

    return storageDirectory;
}

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

    switch(requestCode)
    {
        case CAMERA_CAPTURE:
            if(resultCode == RESULT_OK)
            {
                Toast t = Toast.makeText(klasorAdiActivity.this,"Photo taken",Toast.LENGTH_SHORT);
                t.show();
                startCapture();
            }
            break;
    }
}

修改

对于那些将来会在这里看的人。永远不要以编程方式跳过请求用户权限。

以下是获得存储空间权限的方法:

首先定义常量

private static String TAG = "StoragePermission";
private static final int REQUEST_WRITE_STORAGE = 112;

第二种许可方法,您将检查是否已授予权限,如果没有请求用户的权限

private void Permission() {
if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    Log.i(TAG, "Permission to record denied");

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {


    } else {
        ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            REQUEST_WRITE_STORAGE);
    }
}
}

第三次许可结果

@Override
public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults)                   {
switch (requestCode) {
    case REQUEST_WRITE_STORAGE: {

        if (grantResults.length == 0
                || grantResults[0] !=
                PackageManager.PERMISSION_GRANTED) {

            Log.i(TAG, "Permission denied");

        } else {

            Log.i(TAG, "Permission granted");

        }
        return;
    }
}
}

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

你可以试试这个..

private Uri imageToUploadUri;

    private File CreateImageFile() throws IOException
    {
        File defaultFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+APP_NAME+"/"+edit.getText().toString());
        if (!defaultFile.exists())
             defaultFile.mkdirs();

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = timeStamp + ".jpg";
        File file = new File(defaultFile,imageFileName);

        //renaming file if exist
        int i = 2; 
        while (file.exists()){
            file = new File(defaultFile, timeStamp + "(" + i + ")" + ".jpg");
            i++;
        }

        return file;
    }



    private void captureCameraImage() {
            Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            //stored the image and get the URI
            imageToUploadUri = Uri.fromFile(CreateImageFile());
            startActivityForResult(chooserIntent, CAMERA_PHOTO);
    }


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

          if (requestCode == CAMERA_PHOTO && resultCode == Activity.RESULT_OK) {
               Toast t = Toast.makeText(klasorAdiActivity.this,"Photo taken",Toast.LENGTH_SHORT);
               t.show();
               if(imageToUploadUri != null){
                    Uri selectedImage = imageToUploadUri;
                    // do what ever you want with this image    
               }
          } 
    }