android:将内部存储中的文件公开保存在目录中

标签 android

我正在尝试将图像公开保存在应用程序文件目录内的目录中。通过公开,我的意思是我将能够对其使用操作 View Intent 。

/** Saves the bitmap to app storage */
    public int SaveBitmap(Bitmap bitmap, String filename, Boolean scan){

        // Save the file
        OutputStream os = null;

        String path = context.getFilesDir() + "/MyAppName/";
        File file = new File(path);
        if(!file.isDirectory())file.mkdirs();
        path += (filename + ".png");
        try {
            os = new BufferedOutputStream(new FileOutputStream(path,true));
            //os = mContext.openFileOutput(, Context.MODE_PRIVATE);
            bitmap.compress(CompressFormat.PNG, 100, os);
            os.flush();
            os.close();
            bitmap.recycle();
        } catch (IOException e) {
            e.printStackTrace();
            return 1; //Failure
        }

        File fileR = new File(path);

        fileR.setReadable(true, false);
        // Show in Gallery
        if(scan) ShowInGallery(filename);

        return 0; // SUCCESS

    }

这段代码没有完成工作,文件仍然在私有(private)模式下创建。

使用:

context.openFileOutput(filename + ".png", Context.MODE_WORLD_READABLE); 

我能够公开打开它们,但我无法将文件保存在其中的目录中。

请注意,我针对的是没有 SD 卡的用户,这就是我不使用外部存储的原因。

我能做什么?

最佳答案

您将要使用 Environment.getExternalStoragePublicDirectory(字符串类型)Environment.getExternalStorageDirectory 作为文件目录的根目录。查看documentation了解更多详情。

编辑:

来自 getExternalStorageDirectory () 下的环境文档:

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

所有“外部”的意思是它在您的应用程序外部或公开。因此,如果 SD 卡可用,则此方法默认为该存储目录。如果设备不使用 SD 卡,则使用设备的内置存储。

关于android:将内部存储中的文件公开保存在目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611288/

相关文章:

android - 收到推送通知后强制停止后 Whatsapp 服务重新启动

android - Android GridView 中的图像发生变化。如何?

java - 如何在 Android 上获取新的 Window 实例?

android - 如何同时为 ListView 添加边框和不同的行颜色

android - Android M 中的应用程序崩溃

android - Listview 不显示在 masterdetailsflow 的 fragment 中

android - 如何在android中阻止安全模式

安卓 : How iterate through views of a expandableList

Android 铃声选择器 Activity 在选定设备上崩溃....特别是 Nexus S

java - 我的 Android 应用程序出现错误