简体   繁体   English

如何始终将应用程序存储到sdcard /外部存储

[英]how to always store application to sdcard/external storage

I am making one android app, for which I want to store the downloaded images to sdcard or external storage. 我正在制作一个Android应用,我想将下载的图像存储到sdcard或外部存储中。 The problem I am facing is that on some phone the images are being stored on sdcard whereas in some phones it is being stored in internal memory, specifically in samsung devices like Galaxy S4. 我面临的问题是,在某些手机上,图像存储在sdcard上,而在某些手机上,图像存储在内部存储器中,特别是在诸如Galaxy S4之类的三星设备中。 Please see the attached images for the file path in S4. 请参阅附件中的图像以获取S4中的文件路径。

File wallpaperDirectory = new File(
                    Environment.getExternalStorageDirectory()
                            + "/TestApp/");
            wallpaperDirectory.mkdirs();
            FileOutputStream out = new FileOutputStream(Environment
                    .getExternalStorageDirectory().toString()
                    + "/TestApp/" + secret + ".jpg");
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            Toast.makeText(
                    getApplicationContext(),
                    "Wallpaper saved sucessfully \n"
                            + Environment.getExternalStorageDirectory()
                                    .toString() + "/TestApp/" + secret
                            + ".jpg", Toast.LENGTH_SHORT).show();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));

在S4上存储

I am making one android app, for which I want to store the downloaded images to sdcard or external storage. 我正在制作一个Android应用,我想将下载的图像存储到sdcard或外部存储中。

The Android SDK offers access to external storage. Android SDK提供对外部存储的访问。 Whether this is "sdcard" or something else is up to the device manufacturer. 这是“ sdcard”还是其他取决于设备制造商。

The problem I am facing is that on some phone the images are being stored on sdcard whereas in some phones it is being stored in internal memory, specifically in samsung devices like Galaxy S4. 我面临的问题是,在某些手机上,图像存储在sdcard上,而在某些手机上,图像存储在内部存储器中,特别是在诸如Galaxy S4之类的三星设备中。

This code stores the images on external storage. 该代码将图像存储在外部存储器中。 It is not storing them on internal storage. 它不会将它们存储在内部存储中。 Whether external storage is removable or not is up to the device manufacturer. 外部存储设备是否可移动取决于设备制造商。

You may wish to consider reading the documentation about external storage . 您不妨考虑阅读有关外部存储的文档

You should also: 您还应该:

  • Not use string concatenation to create File objects. 不要使用字符串串联来创建File对象。 Use the proper File constructor. 使用适当的File构造函数。

  • Store your data somewhere better than in a hardcoded TestApp directory off the root of external storage, such as getExternalFilesDir() or getExternalCacheDir() . 将数据存储在比外部存储根目录下的硬编码TestApp目录更好的位置,例如getExternalFilesDir()getExternalCacheDir() Your approach has been considered poor form for years, as it clutters up the user's external storage. 多年来,您的方法一直被认为是较差的形式,因为它会使用户的外部存储混乱。

  • Use a more-focused approach for updating MediaStore , such as using MediaScannerConnection and scanFile() , rather than sending out an operating system broadcast, as if you were the operating system. 使用更专注的方法来更新MediaStore ,例如使用MediaScannerConnectionscanFile() ,而不是发出操作系统广播,就好像您是操作系统一样。

  • Be sure to do all of this work on a background thread (it is unclear whether you are or are not from the code snipppet). 确保在后台线程上完成所有这些工作(从代码片段中不清楚您是否在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM