6

我正在处理绘图部分,并编写了以下代码将图像保存到指定的相机文件夹。但是,我宁愿使用应用名称创建一个新文件夹并将图像保存到该文件夹​​中。怎么可能做到这一点?

我还想稍后从该特定文件夹中检索图像文件。

谢谢!

当前代码:

     String fileName = "ABC";

      // create a ContentValues and configure new image's data
      ContentValues values = new ContentValues();
      values.put(Images.Media.TITLE, fileName);
      values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
      values.put(Images.Media.MIME_TYPE, "image/jpg");

      // get a Uri for the location to save the file
      Uri uri = getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

      try 
      {                                               
         OutputStream outStream = getContext().getContentResolver().openOutputStream(uri);

         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

         outStream.flush(); // empty the buffer
         outStream.close(); // close the stream
4

2 回答 2

12

试试这个可能对你有帮助。

public void saveImageToExternalStorage(Bitmap image) {
    String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/directoryName";
    try
    {
        File dir = new File(fullPath);
        if (!dir.exists()) {
        dir.mkdirs();
        }
        OutputStream fOut = null;
        File file = new File(fullPath, "image.png");
        if(file.exists())
            file.delete();
        file.createNewFile();
        fOut = new FileOutputStream(file);
        // 100 means no compression, the lower you go, the stronger the compression
        image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    }
    catch (Exception e)
    {
        Log.e("saveToExternalStorage()", e.getMessage());
    }
}
于 2013-01-16T17:48:44.650 回答
-1

经过多次尝试,此方法有效。

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath,AUDIO_RECORDER_FOLDER);

    if(!file.exists()){
            file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
于 2014-03-10T06:57:57.120 回答