简体   繁体   English

捕获的图像未存储在android的特定文件夹中

[英]Captured image is not stored in the specific folder in android

I have created a program to capture the image and that is getting stored into sdcard/dcim/camera folder. 我创建了一个捕获图像的程序,该程序被存储到sdcard / dcim / camera文件夹中。 Now I am trying to save the captured image in my own directory created in sdCard, say "/somedir". 现在,我尝试将捕获的图像保存在我自己在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();
            }

Refer the below code 请参考以下代码

    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();
        }
       }
      }

You are settings the wrong file name for the file. 您为文件设置了错误的文件名。 Just use this method if you want to use time in the name of image file. 如果要在图像文件名中使用时间,请使用此方法。

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();
}

Hers is what you need to do: instead of 她需要做的是:

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

do this 做这个

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

Make sure you use the neccessary permissions in your Manifest: 确保在清单中使用必要的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM