【问题标题】:Images are not being saved in my camera app图像未保存在我的相机应用程序中
【发布时间】:2017-01-04 06:42:05
【问题描述】:

我正在尝试制作一个检测运动并在检测到运动时拍照的应用。当我不尝试将图片保存在目录(文件夹)中时,它会保存图片。但是当我尝试使用目录时,即使目录创建成功,图片也没有保存。 我应该对以下代码进行哪些更改才能使其正常工作:

 private void createDirectoryAndSaveFile(String name, Bitmap bitmap) {

        File folder = new File(Environment.getExternalStorageDirectory() +
            File.separator + "XYX APP");
             boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
        if (success) {
        // Do something on success

    } else {
        // Do something else on failure
    }

        File photo = new File(new File(Environment.getExternalStorageDirectory()+"XYZ APP/"), name+ ".jpg");
        if (photo.exists()) {
            photo.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(photo.getPath());
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

注意:文件名按如下指令生成:

 String name = "MotDet_"+String.valueOf(System.currentTimeMillis());
            if (bitmap != null) createDirectoryAndSaveFile(name, bitmap);

更新 它适用于以下代码,但不适用于上面的代码:

    private void save(String name, Bitmap bitmap) {
        File photo = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
        if (photo.exists()) photo.delete();

        try {
            FileOutputStream fos = new FileOutputStream(photo.getPath());
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        } catch (java.io.IOException e) {
            Log.e("PictureDemo", "Exception in photoCallback", e);
        }
    }

【问题讨论】:

  • 你在 marshmello 中测试吗?
  • @Jayanth Yes on s7 edge
  • @Jayanth 你看到我更新的问题了吗?它适用于该代码,但在我尝试制作目录时不起作用
  • 尝试替换行 File photo = new File(new File(Environment.getExternalStorageDirectory()+"XYZ APP/"), name+".jpg");与我的答案中的行
  • 用这一行 File photo = new File(folder.getAbsolutePath()+"/XYZ APP/"+ name+ ".jpg");

标签: java android android-camera android-sdcard android-image


【解决方案1】:

首先你错过了 xyz 之前的 FileSeperator

 File photo = new File(folder.getAbsolutePath()+"/XYZ APP/"+ name+ ".jpg");

你的函数变成了

private void createDirectoryAndSaveFile(String name, Bitmap bitmap) {

File folder = new File(Environment.getExternalStorageDirectory() +
        File.separator + "XYZ APP");//here you have created different name
boolean success = true;
if (!folder.exists()) {
    success = folder.mkdirs();
}
if (success) {
    // Do something on success

} else {
    // Do something else on failure
}

File photo = new File(folder.getAbsolutePath(), name+ ".jpg"); 
if (photo.exists()) {
    photo.delete();
}
try {
    FileOutputStream out = new FileOutputStream(photo.getPath());
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

}

Marshmello 带有 RuntimePermissions 以便您将文件保存在您需要先请求权限的外部目录中,如下面的代码

public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true;
    } else {

        Log.v(TAG,"Permission is revoked");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        return false;
    }
}
else { //permission is automatically granted on sdk<23 upon installation
    Log.v(TAG,"Permission is granted");
    return true;
}

}

权限结果回调

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
       if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
          Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
          //resume tasks needing this permission
      }
   }

在保存之前调用isStoragePermissionsGranted(),如果它返回true继续保存文件。

【讨论】:

  • 事实上我尝试使用 API 22。当我尝试将文件保存在 sd 卡的根目录中时,它会保存文件。但是当我在文件夹中尝试时它不会。
  • 您在 XYZ 尝试添加之前错过了文件分隔符“/”
  • 我尝试添加文件分隔符。它仍然没有保存。 :(
  • 我没有看到它兄弟.. absolutepath 行不在你的答案中,它在 cmets 中
  • 你看到我的这个问题了吗? stackoverflow.com/questions/41798795/…
【解决方案2】:

试试这个代码:

String partFilename = currentDateFormat();
storeCameraPhotoInSDCard(bp, partFilename);

private String currentDateFormat(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
        String  currentTimeStamp = dateFormat.format(new Date());
        return currentTimeStamp;
    }

private void storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate){
        File outputFile = new File(Environment.getExternalStorageDirectory(), "photo_" + currentDate + ".jpg");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

此代码对我有用..将图像保存到目录。

【讨论】:

【解决方案3】:

您必须在运行时在android 6.0 及更高版本中获取permission 的外部存储空间才能写入SDCard

阅读Run time Permission

添加 ma​​nifest.xml

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

用下面的函数替换你的函数

private void createDirectoryAndSaveFile(String name, Bitmap bitmap) {

    File folder = new File(Environment.getExternalStorageDirectory() +
            File.separator + "XYZ APP");//here you have created different name
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
    if (success) {
        // Do something on success

    } else {
        // Do something else on failure
    }

    File photo = new File(folder.getAbsolutePath(), name+ ".jpg"); //use path of above created folder
    if (photo.exists()) {
        photo.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(photo.getPath());
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 我已经做到了。当我尝试在 sd 卡的根目录中执行此操作时,它会保存文件。但是当我在文件夹中尝试时它没有
  • 你在这里忘记了分隔符Environment.getExternalStorageDirectory()+"XYZ APP/"), name+ ".jpg")
  • 我尝试添加文件分隔符。它仍然没有保存。 :(
  • 你看到我stackoverflow.com/questions/41798795/…的这个问题了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 2015-08-04
相关资源
最近更新 更多