【问题标题】:Saving images on different Android devices (phone and tablet)在不同的 Android 设备(手机和平板电脑)上保存图像
【发布时间】:2015-09-02 07:12:19
【问题描述】:

app的主要目标是使用URL从网络图像下载。目前图像下载良好,URL 链接运行良好。但是问题发生在将图像保存到设备存储中。 1.我的手机有SD卡,保存效果很好。 2. 我的平板电脑内置了 16Gb 的存储空间。在我的平板电脑上,图片是下载的,但没有保存。

图片保存源代码示例:

        FileManager fileManager = new FileManager();

        if (fileManager.fileExists(filename)) {
            File file = fileManager.getFile(filename);
            Uri fileUri = Uri.fromFile(file);
            return new SaveImageRequest(fileUri);
        } else {
            String type = ".png"; //fallback to ".png"
            if (path.toString().lastIndexOf(".") != -1) { //-1 means there are no punctuations in the path
                type = path.toString().substring(path.toString().lastIndexOf("."));
            }
            DownloadManager.Request request = new DownloadManager.Request(path);
            request.setTitle(notificationTitle);
            request.setVisibleInDownloadsUi(false);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
            request.allowScanningByMediaScanner();
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, "/DestinationDir/" + filename + type);

            return new SaveImageRequest(downloadManager.enqueue(request));
        }

您是否有任何解决方案可以将图像保存在具有内置存储设备和具有外部存储设备的设备上? PS:android访问存储的权限

【问题讨论】:

  • 看来你应该包括你的SaveImageRequest

标签: java android android-sdcard android-external-storage


【解决方案1】:

您应该按照Google Android Guide 中的说明检查媒体可用性

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    return true;
}
return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
    Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    return true;
  }
return false;
}

【讨论】:

    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多