您的位置:首页 > 移动开发 > Android开发

Android格式化外部设备如USB等

2015-06-09 20:41 399 查看
最主要的方法

/**
     * @Description 格式化
     * @param path 路径
     * @return true success ;false failure
     */
    public boolean formatMedia(final String path) {
        IMountService mountService =  getMountService();
        int result = 0;
        try {
            //当卸载完sdcard后才能格式化sdcard - sdcard can be formatted only after the sdcard is unloaded
            mountService.unmountVolume(path, true, false);
            //给sdcard卸载的时间不能超过5s,超过5s后iMountService.formatVolume()不起作用。
            Thread.sleep(4 * 1000);
            result = mountService.formatVolume(path); //这步就是格式化
            Log.d(TAG, "---------volume's path is " + path + "------format success ? " + result);
            if (result == 0) { //如果成功卸载
                Thread.sleep(4 * 1000);
                int resultMount = mountService.mountVolume(path); //挂载回去
                Log.d(TAG, "---------result_mount " + resultMount); //挂载结果
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (result != 0) {
            return true;
        }
        return false;
    }

    /**
     * @Description 得到IMountService
     * @return IMountService
     */
    public synchronized IMountService getMountService() {
        if (mMountService == null) {
            IBinder iBinder = ServiceManager.getService("mount");
            if (iBinder != null) {
                 mMountService = IMountService.Stub.asInterface(iBinder);
                if (mMountService == null) {
                    Log.e(TAG, "Unable to connect to mount service! - is it running yet?");
                }
            }
        }
        return mMountService;
     }


注意import

import android.os.IBinder;
import android.os.storage.IMountService;
import android.os.ServiceManager;


如果是要格式化所有设备,则可以用固有方法
StorageVolume[] volumes = mountService.getVolumeList();
获取储存信息,再用每一个
volume.getPath();
获得所有路径。

/**
     * @Description format all the device
     */
    public void formatMediaAll() {
        IMountService mountService =  getMountService();
        try {
            StorageVolume[] volumes = mountService.getVolumeList();
            for (StorageVolume volume : volumes) {
                formatMedia(volume.getPath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


注意import

import android.os.storage.StorageVolume;


上述代码验证有效,但是注意只能在源码中编译,直接在eclipse中写会报错,无法编译通过。

如何在源码编译

前提条件:服务器有源码可编译

CRT命令:
    快速压缩:tar xvzf 压缩包名
    快速删除:rm
    快速重命名:mv 文件名.后缀 新文件名.后缀


首先要编译一下SDK(第一次需要,以后不需要)

1.进入源码目录

cd xxxxxxxxxxx/

2.整体编译SDK

source autocompile.sh

然后编译APP

1.从SecureCRT登陆服务器后,先切换到源码目录

cd xxxxxxxxxxx/

2.配置环境

source build/envsetup.sh

lunch XXXXXX-eng

注:以上步骤每次登陆编译机器,切换shell后,都需要执行上述操作配置环境变量

3.切换到项目文件夹,项目需包含配置好相关信息的Android.mk,并去掉bin包和gen包等无用信息,否则编译不通过

cd packages/apps/MyProjectName/

4.运行

mm

5.去输出文件夹找到apk

../out/target/product/XXXDevice/system/app/MyProject.apk

附件:完整代码VolumeManager.java

/******************************************************************
*
* @author: AZZ
*
* @version: 1.0.0
*
* Create at: 2015年6月8日 下午4:45:22
*
* Revision:
*
* 2015年6月8日 下午4:45:22
* - first revision
*
*****************************************************************/

import android.content.Context;
import android.os.IBinder; import android.os.storage.IMountService; import android.os.ServiceManager;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.os.storage.StorageEventListener;
import android.util.Log;

/**
* @ClassName VolumeManager
* @Description 管理格式化的类
* @author AZZ
* @Date 2015年6月8日 下午4:45:22
* @version 1.0.0
*/
public final class VolumeManager {
/**
* @Field @TAG : TAG
*/
public static final String TAG = VolumeManager.class.getSimpleName();
/**
* @Field @mMountService : mMountService
*/
private IMountService mMountService;
/**
* @Field @unloadedDuration : unloaded device duration
*/
private final int unloadedDuration = 4 * 1000;
/**
* @Field @mStorageManager : 存储管理者
*/
private StorageManager mStorageManager;
/**
* @Field @mStorageListener : 监听存储状态改变事件
*/
private StorageEventListener mStorageListener = new StorageEventListener() {
@Override
public void onStorageStateChanged(final String path, final String oldState, final String newState) {
Log.i(TAG, "Received storage state changed notification that \""
+ path + "\" changed state from [" + oldState + "] to [" + newState + "]");
}
};
/**
* @Description init
* @param context context
*/
public VolumeManager(final Context context) {
mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
mStorageManager.registerListener(mStorageListener);
}
/**
* @Description 格式化
* @param path 路径
* @return true success ;false failure
*/
public boolean formatMedia(final String path) {
IMountService mountService = getMountService();
int result = 0;
try {
//当卸载完sdcard后才能格式化sdcard - sdcard can be formatted only after the sdcard is unloaded
mountService.unmountVolume(path, true, false);
//给sdcard卸载的时间不能超过5s,超过5s后iMountService.formatVolume()不起作用。
Thread.sleep(unloadedDuration);
result = mountService.formatVolume(path);
Log.d(TAG, "---------volume's path is " + path + "------format success ? " + result);
if (result == 0) {
Thread.sleep(unloadedDuration);
int resultMount = mountService.mountVolume(path); //挂载回去
Log.d(TAG, "---------result_mount " + resultMount);
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (result != 0) {
return true;
}
return false;
}
/**
* @Description format all the device
*/
public void formatMediaAll() {
IMountService mountService = getMountService();
try {
StorageVolume[] volumes = mountService.getVolumeList();
for (StorageVolume volume : volumes) {
boolean result = formatMedia(volume.getPath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Description 得到IMountService
* @return IMountService
*/
public synchronized IMountService getMountService() {
if (mMountService == null) {
IBinder iBinder = ServiceManager.getService("mount");
if (iBinder != null) {
mMountService = IMountService.Stub.asInterface(iBinder);
if (mMountService == null) {
Log.e(TAG, "Unable to connect to mount service! - is it running yet?");
}
}
}
return mMountService;
}
/**
* @Description 销毁方法
*/
public void destroy() {
if (mStorageManager != null) {
mStorageManager.unregisterListener(mStorageListener);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: