/*
* Copyright (C) 2014 NextApp, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package nextapp.mediafile;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.MediaStore;
/**
* Wrapper for manipulating files via the Android Media Content Provider. As of Android 4.4 KitKat, applications can no longer write
* to the "secondary storage" of a device. Write operations using the java.io.File API will thus fail. This class restores access to
* those write operations by way of the Media Content Provider.
*
* Note that this class relies on the internal operational characteristics of the media content provider API, and as such is not
* guaranteed to be future-proof. Then again, we did all think the java.io.File API was going to be future-proof for media card
* access, so all bets are off.
*
* If you're forced to use this class, it's because Google/AOSP made a very poor API decision in Android 4.4 KitKat.
* Read more at https://plus.google.com/+TodLiebeck/posts/gjnmuaDM8sn
*
* Your application must declare the permission "android.permission.WRITE_EXTERNAL_STORAGE".
*/
public class MediaFile {
private final File file;
private final ContentResolver contentResolver;
private final Uri filesUri;
private final Uri imagesUri;
public MediaFile(ContentResolver contentResolver, File file) {
this.file = file;
this.contentResolver = contentResolver;
filesUri = MediaStore.Files.getContentUri("external");
imagesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
/**
* Deletes the file. Returns true if the file has been successfully deleted or otherwise does not exist. This operation is not
* recursive.
*/
public boolean delete()
throws IOException {
if (!file.exists()) {
return true;
}
boolean directory = file.isDirectory();
if (directory) {
// Verify directory does not contain any files/directories within it.
String[] files = file.list();
if (files != null && files.length > 0) {
return false;
}
}
String where = MediaStore.MediaColumns.DATA + "=?";
String[] selectionArgs = new String[] { file.getAbsolutePath() };
// Delete the entry from the media database. This will actually delete media files (images, audio, and video).
contentResolver.delete(filesUri, where, selectionArgs);
if (file.exists()) {
// If the file is not a media file, create a new entry suggesting that this location is an image, even
// though it is not.
ContentValues values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DATA, file.getAbsolutePath());
contentResolver.insert(imagesUri, values);
// Delete the created entry, such that content provider will delete the file.
contentResolver.delete(filesUri, where, selectionArgs);
}
return !file.exists();
}
public File getFile() {
return file;
}
/**
* Creates a new directory. Returns true if the directory was successfully created or exists.
*/
public boolean mkdir()
throws IOException {
if (file.exists()) {
return file.isDirectory();
}
ContentValues values;
Uri uri;
// Create a media database entry for the directory. This step will not actually cause the directory to be created.
values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DATA, file.getAbsolutePath());
contentResolver.insert(filesUri, values);
// Create an entry for a temporary image file within the created directory.
// This step actually causes the creation of the directory.
values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DATA, file.getAbsolutePath() + "/temp.jpg");
uri = contentResolver.insert(imagesUri, values);
// Delete the temporary entry.
contentResolver.delete(uri, null, null);
return file.exists();
}
/**
* Returns an OutputStream to write to the file. The file will be truncated immediately.
*/
public OutputStream write()
throws IOException {
if (file.exists() && file.isDirectory()) {
throw new IOException("File exists and is a directory.");
}
// Delete any existing entry from the media database.
// This may also delete the file (for media types), but that is irrelevant as it will be truncated momentarily in any case.
String where = MediaStore.MediaColumns.DATA + "=?";
String[] selectionArgs = new String[] { file.getAbsolutePath() };
contentResolver.delete(filesUri, where, selectionArgs);
ContentValues values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DATA, file.getAbsolutePath());
Uri uri = contentResolver.insert(filesUri, values);
if (uri == null) {
// Should not occur.
throw new IOException("Internal error.");
}
return contentResolver.openOutputStream(uri);
}
}
分享到:
相关推荐
### Android中SDCard的读写操作详解 #### 一、SDCard访问概述 在Android开发过程中,对于外部存储(如SDCard)的读写是非常常见的需求。本文将详细介绍如何在Android应用中对SDCard进行读写操作,包括获取缓存目录...
总的来说,“Fix安卓4.4 扩展sdcard 不能写补丁”是一个旨在解决4.4系统SD卡写权限问题的解决方案,它通过修改系统的某些关键设置,恢复用户对扩展存储的完全控制。然而,由于涉及到系统级别的更改,使用前应谨慎...
本实例将探讨如何在Android应用中实现SdCard的读取和写入功能,这涉及到Android的权限管理、文件操作API以及外部存储的相关知识。 1. **权限管理**: 在AndroidManifest.xml文件中,你需要添加`READ_EXTERNAL_...
### Android平台上SDCard的自动mount的解决方法 在Android设备中,SDCard(Secure Digital Card)作为外部存储介质被广泛使用。为了使Android系统能够自动识别并挂载SDCard,需要进行一系列配置与调试工作。本文将...
在Android系统中,获取SDCard(外部存储)的内存信息是开发者经常遇到的任务,这有助于管理应用程序的存储需求,比如保存用户数据、缓存文件或者下载内容。标题提到的"Android解析如何获取SDCard内存项目源代码"显然...
在Android系统中,由于安全性和隐私保护的考虑,对应用程序访问外部存储卡(SDcard)的权限有着严格的控制。在Android 6.0(API级别23)之后,系统引入了运行时权限管理机制,应用需要在运行时请求访问SDcard的权限...
在Android平台上,对SDCard(外部存储)进行文件读写是常见的操作,尤其对于需要存储大量数据或用户数据的应用来说。这个"Android应用源码SdCard读写文件实例.zip"压缩包提供了一个示例,帮助开发者理解如何在...
如何在 android 模拟器中使用SDCard
Sdcard(Secure Digital Card)是Android设备常用的外部存储器,用于存放大量的非敏感数据,如媒体文件、应用缓存等。本篇文章将深入探讨Android如何在Sdcard上进行数据存储,并通过一个简单的示例来说明。 首先,...
其中SD卡(Secure Digital Card,简称SDCard)在Android设备中主要用作存储扩展,它可以用来存放各种数据文件,比如图片、音乐和视频等。在Android模拟器中模拟SD卡的使用主要包含以下几个方面知识点: 首先,了解...
在Android系统中,外置SDCard(也称为外部存储)是设备上用于扩展存储空间的区域,用户可以在这个空间上存储各种数据,如媒体文件、应用程序数据等。本篇文章将详细探讨如何在Android应用中实现对外置SDCard的读取、...
本篇将重点讲解如何在Android中进行文件读写,包括对内部存储和外部存储(即SDCard)的操作。 首先,了解Android的存储体系至关重要。Android设备有两种主要的存储类型:内部存储和外部存储。内部存储通常用于应用...
/ 参数说明 // context:上下文 // dialogid:对话框ID // title:对话框标题 // callback:一个传递Bundle参数的回调接口 // suffix:需要选择的文件后缀,比如需要选择wav、mp3文件的时候设置为".wav;...
在Android平台上,对SDCard(外部存储)进行文件操作是应用程序与用户交互的重要部分,尤其对于需要存储大量数据或媒体文件的应用来说。本教程将详细解释如何在Android中读写SDCard文件,非常适合初学者入门。 首先...
android应用开发中关于SDCard读写操作的实例代码,在下载学习SDCard操作中,碰到很棘手的问题了。不过最后还在我的坚持下成功了,
在Android系统中,获取OTG(On-The-Go)U盘的路径以及内部或外部SD卡的路径是进行文件操作的基础。这篇详细解释将围绕如何在Android M及以上版本的设备上实现这一功能,并提供一个简单实用的代码示例。 首先,我们...
- **定义**:内部存储是Android系统为每个应用分配的私有存储空间,其他应用无法直接访问,只有拥有该应用的用户权限才能读写。 - **路径**:通常,内部存储的根目录为`/data/data/your_package_name/`,这里的`...