【问题标题】:Save Images to specific Folder in Phone Android [duplicate]将图像保存到Android手机中的特定文件夹[重复]
【发布时间】:2016-04-12 10:21:29
【问题描述】:

我想捕捉图像并将其保存到特定文件夹而不是 DCIM/相机或图库中...

想保存如下:storage/sdcard0/DCIM/MyFolder。

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: android


【解决方案1】:

试试这个可能对你有帮助

public void takePicture(){
        Intent imgIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "ImagesApp");
        imagesFolder.mkdirs(); // <----
        File image = new File(imagesFolder, "temp.jpg");
        Uri uriSavedImage = Uri.fromFile(image);
        imgIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
        startActivityForResult(imgIntent,IMAGE_CAPTURE_REQUEST_CODE);
    }

【讨论】:

    【解决方案2】:

    您可以使用以下代码

    private String save(Bitmap bitmap)
    {
            File save_path = null;
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            {
                try
                {
                    File sdCard = Environment.getExternalStorageDirectory();
                    File dir = new File(sdCard.getAbsolutePath() + "/DirName");
                    dir.mkdirs();
                    File file = new File(dir, "DirName_"+new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime())+ ".png");
                    save_path =   file;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100,baos);
                    FileOutputStream f = null;
                    f = new FileOutputStream(file);
                    MediaScannerConnection.scanFile(this, new String[]{file.getAbsolutePath()}, null, null);
                    if (f != null)
                    {
                        f.write(baos.toByteArray());
                        f.flush();
                        f.close();
                    }
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                }
            }
            return String.valueOf(save_path);
        }
    

    希望对你有所帮助...

    【讨论】:

    • 我只想将图像保存在 DirName 文件夹中,它也保存在 Camera 文件夹中......
    【解决方案3】:

    试试下面的代码

    private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {
    
    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");
    
    if (!direct.exists()) {
        File wallpaperDirectory = new File("/sdcard/DirName/");
        wallpaperDirectory.mkdirs();
    }
    
    File file = new File(new File("/sdcard/DirName/"), fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 2013-08-03
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      相关资源
      最近更新 更多