android开发入门教程

android开发入门教程
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

如何往外置sd卡中写入数据

Posted on 2014-10-17 10:31  android开发入门教程  阅读(2063)  评论(0)    收藏  举报

============问题描述============


在学习android开发   看了老罗的视频   
结果是如果用Environment.getExternalStorageDirectory()获取sd卡路径存入文件后文件都是写入到的自带的内置内存卡中,
然后输入这个路径后得到的是/storage/sdcard0    
就想咨询一下  难道Environment.getExternalStorageDirectory()获取外置内存卡的路径而是内置内存卡路径?
如果如此的话那如何往外置内存卡中写入文件?
做测试用的机器是三星的平板 

============解决方案1============


package com.cshis.DGRHotel.SDCardReader;



import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;



import com.cshis.DGRHotel.SDCardReader.Dev_MountInfo.DevInfo;



import android.os.Environment;

/**

 * 取外置SD卡路径类

 * @author WangYimin

 *

 */

public class Dev_MountInfo implements IDev {

	/**

	 * ***

	 */

	public final String HEAD = "dev_mount";

	public final String LABEL = "<label>";

	public final String MOUNT_POINT = "<mount_point>";

	public final String PATH = "<part>";

	public final String SYSFS_PATH = "<sysfs_path1...>";



	/**

	 * Label for the volume

	 */

	private final int NLABEL = 1;

	/**

	 * Partition

	 */

	private final int NPATH = 2;

	/**

	 * Where the volume will be mounted

	 */

	private final int NMOUNT_POINT = 3;

	private final int NSYSFS_PATH = 4;



	private final int DEV_INTERNAL = 0;

	private final int DEV_EXTERNAL = 1;



	private ArrayList<String> cache = new ArrayList<String>();



	private static Dev_MountInfo dev;

	private DevInfo info;



	private final File VOLD_FSTAB = new File(Environment.getRootDirectory()

			.getAbsoluteFile()

			+ File.separator

			+ "etc"

			+ File.separator

			+ "vold.fstab");



	public static Dev_MountInfo getInstance() {

		if (null == dev)

			dev = new Dev_MountInfo();

		return dev;

	}



	private DevInfo getInfo(final int device) {

		// for(String str:cache)

		// System.out.println(str);



		if (null == info)

			info = new DevInfo();



		try {

			initVoldFstabToCache();

		} catch (IOException e) {

			e.printStackTrace();

		}



		if (device >= cache.size())

			return null;

		String[] sinfo = cache.get(device).split(" ");



		info.setLabel(sinfo[NLABEL]);

		info.setMount_point(sinfo[NMOUNT_POINT]);

		info.setPath(sinfo[NPATH]);

		info.setSysfs_path(sinfo[NSYSFS_PATH]);



		return info;

	}



	/**

	 * init the words into the cache array

	 * @throws IOException

	 */

	private void initVoldFstabToCache() throws IOException {

		cache.clear();

		BufferedReader br = new BufferedReader(new FileReader(VOLD_FSTAB));

		String tmp = null;

		while ((tmp = br.readLine()) != null) {

			// the words startsWith "dev_mount" are the SD info

			if (tmp.startsWith(HEAD)) {

				cache.add(tmp);

			}

		}

		br.close();

		cache.trimToSize();

	}



	public class DevInfo {

		private String label, mount_point, path, sysfs_path;



		/**

		 * return the label name of the SD card

		 * @return

		 */

		public String getLabel() {

			return label;

		}



		private void setLabel(String label) {

			this.label = label;

		}



		/**

		 * the mount point of the SD card

		 * @return

		 */

		public String getMount_point() {

			return mount_point;

		}



		private void setMount_point(String mount_point) {

			this.mount_point = mount_point;

		}



		/**

		 * SD mount path

		 * @return

		 */

		public String getPath() {

			return path;

		}



		private void setPath(String path) {

			this.path = path;

		}



		/**

		 * "unknow"

		 * @return

		 */

		public String getSysfs_path() {

			return sysfs_path;

		}



		private void setSysfs_path(String sysfs_path) {

			this.sysfs_path = sysfs_path;

		}



	}



	@Override

	public DevInfo getInternalInfo() {

		return getInfo(DEV_INTERNAL);

	}



	@Override

	public DevInfo getExternalInfo() {

		return getInfo(DEV_EXTERNAL);

	}

}



interface IDev {

	DevInfo getInternalInfo();



	DevInfo getExternalInfo();

}





调用方法:
	Dev_MountInfo dev = Dev_MountInfo.getInstance();

		DevInfo info = dev.getInternalInfo();// Internal SD Card Informations

		info = dev.getExternalInfo();// External SD Card Informations



		PATH.ROOT_SDCARD_PATH = info.getPath();

		PATH.ROOT_INSIDE_PATH = Environment.getExternalStorageDirectory()

				.getAbsolutePath();

============解决方案2============


原生的android系统,Environment.getExternalStorageDirectory()获取的是外部的sd卡,三星的应该都内置一个sd卡,程序默认读到的都是内置的,外置的sd卡的话,应该是三星另外做了个接口出来的,可以反射查看下Environment类下的接口方法就知道了

============解决方案3============


引用 6 楼 klordy_123 的回复:
问题最新进度:  按照同学提的意见  因为开发的特殊性   所以直接用了绝对路径   
可是在绝对路径生成的file中, 无法生成第二层文件夹    即比如先在外置内存卡中已经生成了A这个文件夹,但是在A的文件夹之下无法生成子文件夹  这是为什么? 感觉像是无法对A这个文件夹之下进行任何写入操作一样   这是为什么?


文件夹没有时要自己创建的

============解决方案4============


印象中Android默认的外置内存卡的路径是“/storage/sdcard0”(“/mnt/sdcard”也可以) 可能三星把路径改了,可是获得外置内存卡路径的方法还是用Google原生的。
我要啦免费统计