【问题标题】:How to save image and video captured from camera in App internal memory only in Android? [duplicate]如何仅在Android中将相机拍摄的图像和视频保存在应用程序内存中? [复制]
【发布时间】:2013-04-22 05:59:19
【问题描述】:

我正在进行一个项目,我需要将图像和视频仅保存在应用程序内部存储器中(而不是在 SDCard 或设备库中)。我正在使用下面的代码。但这也将图像/视频保存到设备库。请指教。

Bitmap bm;
        View v=imageview;
        v.setDrawingCacheEnabled(true);
        bm=Bitmap.createBitmap(v.getDrawingCache());
        v.setDrawingCacheEnabled(false);
        String fileName="image.png";        
        try 
        {
            FileOutputStream fOut=openFileOutput(fileName, MODE_PRIVATE);
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);

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

我想将图片和视频设为私密且安全。所以我想将它们保存在应用程序内部存储器中。所以没有其他应用程序可以访问它。请建议。

【问题讨论】:

  • 我不明白。应用程序内部存储器是什么意思(不在 SD 卡或设备库中)?你能告诉我吗?
  • @RajaReddyPolamReddy 这提供了将图像保存在设备 SDCard 中的机制。所以所有应用程序都可以访问它。我想让图像和视频变得私密和安全。所以我想将它们保存在应用程序内部存储器中。所以没有其他应用程序可以访问它。请提出建议。
  • @sanchitsingh 检查我的答案,它对我有用。

标签: android video-capture android-gallery android-image


【解决方案1】:

将图像存储在内存中,它将图像存储在内部图像文件夹中。

                    Uri uriSavedImage;

                // the temp folder to start the camera activity with
                    String fileName = "image_" + String.valueOf(imageNum)+ ".png";
                    path = getDir("images", Context.MODE_WORLD_WRITEABLE).getPath() + "/" + fileName;
                    // start the camera activity
                    file = new File(path);
                    while (file.exists()) {
                        imageNum++;
                        fileName = "image_" + String.valueOf(imageNum)+ ".png";     
                        path = getDir("images_pho",Context.MODE_WORLD_WRITEABLE).getPath()+ "/" + fileName;
                        file = new File(path);
                    }
                    Uri ur = Uri.parse(file.toString());
                    uriSavedImage = Uri.fromFile(file);

            Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

            OutputStream imageFileOS;
            try {
                imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
                imageFileOS.write(data);
                imageFileOS.flush();
                imageFileOS.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

希望对你有所帮助

【讨论】:

  • 我检查了代码。但它也将进入设备库。我们不能让它保存在其他应用无法访问的地方吗?
  • 你用设备或模拟器检查过吗?
  • 我在设备中检查过。
  • 具有内部存储器的设备现在可以通过移动配置访问...所以无论您在哪里拍摄图像,它都会在图库中...过去的移动设备无法访问...如果我有解决方案意味着我会打电话给你!
【解决方案2】:

正如我告诉你的,您可以将相机拍摄的照片存储在内部存储中,然后您可以立即使用getContentResolver().delete(uri, null, null); 从图库中删除拍摄的图像,或者您可以使用它:

if (takenpicturefile.exists())
        if (takenpicturefile.delete())
            Log.d("tag","filedeleted");

更多详情,您可能需要参考this。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2021-01-10
    • 2012-09-10
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多