0

我创建了一个程序来捕获图像并将其存储到 sdcard/dcim/camera 文件夹中。现在我正在尝试将捕获的图像保存在我自己在 sdCard 中创建的目录中,比如“/somedir”。

I am able to make the directory programmatically but the image file is not getting stored in it.

Can anybody tell me where I am doing wrong here??

Here is the code....


File folder = new File(Environment.getExternalStorageDirectory() + "/abc");

Bitmap mybitmap1;     //mybitmap1 contain image. So plz dont consider that I don't have image in mybitmap1;
if(!folder.exists())
        {
            success = folder.mkdir();
            Log.i("Log", "folder created");
        }
        else
        {
            Log.i("Log", "Folder already present here!!");
        }
        String fname = date +".jpg";
        file = new File( folder,fname);
        if (file.exists ()) 
            file.delete (); 
        capturedImageUri = Uri.fromFile(file);

        FileOutputStream out;
            byte[] byteArray = stream.toByteArray();
            try {

                   out = new FileOutputStream(file);
                   mybitmap1.compress(Bitmap.CompressFormat.JPEG, 100, out);
                   out.flush();
                   out.close();
                   MediaStore.Images.Media.insertImage(getContentResolver(), mybitmap1, file.getName(), file.getName());
                   //MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());


            } catch (Exception e) {
                   e.printStackTrace();
            }
4

3 回答 3

3

参考下面的代码

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

    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && requestCode == 1 ) {

    final Uri selectedImage = data.getData(); 

    try {
            bitmap = Media.getBitmap(getContentResolver(),selectedImage);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

            File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator
                    + filename);
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       }
      }
于 2012-07-13T11:09:42.113 回答
1

您正在为文件设置错误的文件名。如果您想在图像文件的名称中使用时间,请使用此方法。

private Uri getImageUri() {
    // Store image in dcim
    String currentDateTimeString = getDateTime();
    currentDateTimeString = removeChar(currentDateTimeString, '-');
    currentDateTimeString = removeChar(currentDateTimeString, '_');
    currentDateTimeString = removeChar(currentDateTimeString, ':');
    currentDateTimeString = currentDateTimeString.trim();
    File file = new File(Environment.getExternalStorageDirectory()
            + "/DCIM", currentDateTimeString + ".jpg");
    Uri imgUri = Uri.fromFile(file);

    return imgUri;
}


private final static String getDateTime() {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
    df.setTimeZone(TimeZone.getTimeZone("PST"));
    return df.format(new Date());
}

public static String removeChar(String s, char c) {
    StringBuffer r = new StringBuffer(s.length());
    r.setLength(s.length());
    int current = 0;
    for (int i = 0; i < s.length(); i++) {
        char cur = s.charAt(i);
        if (cur != c)
            r.setCharAt(current++, cur);
    }
    return r.toString();
}
于 2012-07-13T11:36:54.593 回答
0

她的就是你需要做的:而不是

File folder = new File(Environment.getExternalStorageDirectory() + "/abc");

做这个

File folder = new File(Environment.getExternalStorageDirectory().getPath() + "/abc");
if(folder.exists()){
  //save your file then
}
else{
  folder.mkdirs();
  //save your file then
}

确保在 Manifest 中使用必要的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-07-13T11:54:43.637 回答