【问题标题】:Image directory not created when i take picture from my app从我的应用程序拍照时未创建图像目录
【发布时间】:2017-02-27 13:49:42
【问题描述】:

我在android studio 工作。构建一个我正在使用相机的app。当我运行我的应用程序时,该应用程序运行良好。我捕获了它确实捕获了图像的图像。但是我创建的文件夹没有显示在我的画廊中。我将图像保存在我的 local storage 中,而不是 SD CARD 中。我很好奇为什么没有创建该文件夹,因为它没有给我任何错误,所以它应该在我的画廊中。所以我重新启动了我的设备,重新启动后我可以看到我画廊中的文件夹和其中拍摄的图像。我再次打开应用程序并从中获取图像,但图像再次未显示在文件夹中。

下面是我的代码,我在其中制作 ta 目录以保存图像

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        if(resultCode == Activity.RESULT_OK)
        {
            Bitmap bmp = (Bitmap)data.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            // convert byte array to Bitmap
            Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

            if(isStoragePermissionGranted())
            {
                SaveImage(bitmap);
            }


        }

}
 private void SaveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
    Log.v(LOG_TAG, root);
    File myDir = new File(root + "/captured_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 1000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir,fname);
    if (file.exists())file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
        out.flush();
        out.close();


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

}

下面是我调试的图片

**注意:**

由于我使用的是本机相机,因此图片保存在相机胶卷文件夹中,即我设备中的默认文件夹。但是那里保存的图像不是压缩的,压缩图像应该保存在我创建的文件夹中。

我坚持下去,不知道该怎么办。

任何帮助将不胜感激。

【问题讨论】:

  • saveImage方法调用这个sendBroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
  • 尝试并收到此错误java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data (has extras) }} to activity
  • 在 Android 4.4 之前 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 从 Android 4.4 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

标签: android image android-camera save-image


【解决方案1】:

你需要调用MediaScannerConnectionscanFile(Context context, String[] paths, String[] mimeTypes, MediaScannerConnection.OnScanCompletedListener callback)方法。

MediaScannerConnection 为应用程序提供了一种将新创建或下载的媒体文件传递给媒体扫描器服务的方法。这将使用新保存的媒体更新您的文件夹。

    private void SaveImage(Bitmap finalBitmap) {

        //....
        if(!storageDir.exists()){
               storageDir.mkdirs();
         }
        //...
        file.createNewFile();
        try {
            MediaScannerConnection.scanFile(context, new String[]  {file.getPath()} , new String[]{"image/*"}, null);
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

【讨论】:

  • in `new String[]{"image/*"});` 代替 image/* 我将放置我的文件夹名称?
  • 您需要放置 MIME 类型的数组。在您的情况下,如果它是唯一的图像媒体,它将是 image/* 本身。
  • 好的,不用改了吗?
  • 仍然无法查看我的画廊中的任何文件夹,现在我得到了这个java.io.FileNotFoundException: /storage/emulated/0/captured_images/Image-951.jpg: open failed: ENOENT (No such file or directory),它正在点击FileOutputStream out = new FileOutputStream(file);
  • 我已对答案进行了编辑。仅当目录不存在时才创建目录。同样在确保该文件不存在后,使用 file.createNewFile() 方法。希望这会有所帮助
【解决方案2】:

试试这个

private void saveBitmap(Bitmap bitmap) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = timeStamp + ".jpg";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        final String fileLoc = storageDir.getAbsolutePath() + "/folderName/" + imageFileName;
        File file = new File(fileLoc);
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
        try {`enter code here`
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

【讨论】:

  • 创建文件夹试试这个 File folder = new File("/sdcard/folder"); if (!folder.exists()) 文件夹.mkdirs();
  • 我没有使用SD 卡,我将图像保存在本地存储中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多