图像未保存在我的相机应用程序中

slhcrj9b  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(372)

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

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

hjzp0vay1#

请尝试以下代码:

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

这段代码是我的工作..保存图像到目录。

ozxc1zmp

ozxc1zmp2#

首先你错过了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 继续保存文件。

nbysray5

nbysray53#

你必须得到 permission 中运行时的外部存储 android 6.0 上面写在SD卡上
读取运行时权限
加载项清单.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();
    }
}

相关问题