首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何挂载sdcard程序化?

如何挂载sdcard程序化?
EN

Stack Overflow用户
提问于 2010-09-15 16:56:09
回答 2查看 4.1K关注 0票数 0

我想挂载sdcard程序化,我如何检查?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-15 19:45:25

如果您正在编写普通的SDK应用程序,则无法自行挂载SD卡。

如果您为设备制造商工作,或者您正在构建一个可以使用固件签名密钥进行签名的应用程序,则可以使用USB_MASS_STORAGE_ENABLED

票数 1
EN

Stack Overflow用户

发布于 2012-03-25 23:24:56

为了使其正常工作,您需要添加用户库classes-full-debug.jar (来自aosp或cm版本),在 android.jar之前添加(在构建路径中有一个面板来对jars进行排序),或者StorageManager不会解析registerListener()您还需要android.permission.MOUNT_UNMOUNT_FILESYSTEMS

代码语言:javascript
复制
package x.y.z;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.storage.IMountService;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.os.ServiceManager;
import android.widget.TextView;

public class MyActivity extends Activity
{
    private static final String MOUNTPOINT = "/mnt/sdcard";
    private IMountService mMountService;
    private StorageManager mStorageManager;
    private TextView mText;

    private final StorageEventListener mStorageListener = new StorageEventListener()
    {
        @Override
        public void onStorageStateChanged(String path, String oldState, String newState)
        {
            String text = mText.getText() + "\n";
            text += "state changed notification that " + path + " changed state from " + oldState + " to " + newState;
            mText.setText(text);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mText = (TextView) findViewById(R.id.textView);

        if (mMountService == null)
        {
            IBinder service = ServiceManager.getService("mount");
            mMountService = IMountService.Stub.asInterface(service);
        }

        if (mStorageManager == null)
        {
            mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
            mStorageManager.registerListener(mStorageListener);        
        }

        try
        {
            String state = mMountService.getVolumeState(MOUNTPOINT);
            mText.setText("Media state " + state);
            if (state.equals(Environment.MEDIA_MOUNTED))
                mMountService.unmountVolume(MOUNTPOINT, false);
            else if (state.equals(Environment.MEDIA_UNMOUNTED))
                mMountService.mountVolume(MOUNTPOINT);
        } catch (RemoteException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy()
    {
        if (mStorageManager != null && mStorageListener != null)
            mStorageManager.unregisterListener(mStorageListener);
        super.onDestroy();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3716105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档