【问题标题】:Hide camera images from user gallery从用户图库中隐藏相机图像
【发布时间】:2018-04-11 06:23:36
【问题描述】:

您好,我正在尝试使用户无法访问从我的应用中捕获的图像。首先,我尝试将这些图像保存到不起作用的内部存储中。然后我尝试使用“。”隐藏它们。在文件夹名称前面。我不确定这样做的正确方法是什么。我还尝试创建一个名为 .nomedia 的文件来绕过媒体扫描仪。我对执行此操作的正确方法感到非常困惑。这是我的代码:

  public String getImageUri(Context inContext, Bitmap inImage) {
    /* *//*  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, ".title", null);
    return Uri.parse(path);*//*
     */

    File file = new File(Environment.getExternalStorageDirectory()
            + File.separator + "/.myFolder");
    file.mkdirs();
    File mFile = new File(Environment.getExternalStorageDirectory()
            + File.separator + "/.nomedia");
     mFile.mkdirs();
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(file);
        inImage.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();
       uri =   MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return uri;

}

如果我使用 file.mkdirs(),我会得到 filenotfoundexception。如果我删除该行,我不会收到任何错误,但我的 uri 是空的。

上述函数是否也返回文件路径?我稍后需要文件路径和uri。任何帮助表示赞赏。

【问题讨论】:

  • 从相机捕获后,您将这些文件存储在哪里?可以指定路径吗?
  • 我认为这是我文件的路径。Environment.getExternalStorageDirectory() + File.separator + "/.myFolder"

标签: android image file


【解决方案1】:

我想您不必添加其他扩展程序或其他内容,只需将它们保存在应用程序的外部缓存目录中,图库应用程序将无法读取您的私有目录,除非您通知它们。

因此将您的相机图像存储在这里,没有画廊应用程序可以检测到它。

context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

示例代码

 public static File createPictureFile(Context context) throws IOException {
        Locale locale = Locale.getDefault();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", locale).format(new Date());
        String fileName = "JPEG_" + timeStamp + "_";
        // Store in normal camera directory
        File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        return File.createTempFile(fileName, ".jpg", storageDir);
    }

【讨论】:

【解决方案2】:

将您的图像保存到内部存储中。 MediaScanner 或 Gallery 等其他应用程序无权从您自己的应用程序内存中读取。示例代码:

private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"profile.jpg");

        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
            try {
              fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        } 
        return directory.getAbsolutePath();
    }

【讨论】:

    【解决方案3】:

    用不同的扩展名保存图像。

    例如:.jpg 可以保存为 .ttj。

    【讨论】:

      【解决方案4】:

      试试这个代码。设置您要保存照片的位置。一旦您收到 onActivityResult() 的响应,在所需的 URI 中,您将获得照片。

      public void captureImageFromCamera() {
      
              fileUri = FileUtils.getInstance().getOutputMediaFile(null);
              if(fileUri == null){
                  Utilities.displayToastMessage(ApplicationNekt.getContext(),
                          context.getString(R.string.unable_to_access_image));
                  return;
              }
      
              Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
              startActivityForResult(intent, TAKE_PICTURE);
          }
      

      以下功能将为您提供所需的路径。根据您的项目需要更改它。

      public Uri getOutputMediaFile(String imageNameWithExtension) {
      
              if (imageNameWithExtension == null)
                  imageNameWithExtension = "image_" + System.currentTimeMillis() + ".jpg";
      
              String extPicDir = getExtDirPicturesPath();
              if (!Utilities.isNullOrEmpty(extPicDir)) {
                  File mediaFile = new File(extPicDir, imageNameWithExtension);
                  return Uri.fromFile(mediaFile);
              } else {
                  Log.d("tag", "getOutputMediaFile." +
                          "Empty external path received.");
                  return null;
              }
      
          }
      
      public String getExtDirPicturesPath() {
              File file = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
              if (file != null)
                  return file.getPath();
              else
                  Log.d("", "getExtDirPicturesPath failed. Storage state: "
                          + Environment.getExternalStorageState());
              return null;
          }
      

      获取结果照片。

      public void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
      
              if (resultCode != Activity.RESULT_OK)
                  return;
      
              switch (requestCode) {
                  case TAKE_PICTURE:
                      //fileuri variable has the path to your image.
                      break;
               }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多