我想挂载sdcard程序化,我如何检查?
发布于 2010-09-15 19:45:25
如果您正在编写普通的SDK应用程序,则无法自行挂载SD卡。
如果您为设备制造商工作,或者您正在构建一个可以使用固件签名密钥进行签名的应用程序,则可以使用USB_MASS_STORAGE_ENABLED。
发布于 2012-03-25 23:24:56
为了使其正常工作,您需要添加用户库classes-full-debug.jar (来自aosp或cm版本),在 android.jar之前添加(在构建路径中有一个面板来对jars进行排序),或者StorageManager不会解析registerListener()您还需要android.permission.MOUNT_UNMOUNT_FILESYSTEMS
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();
}
}https://stackoverflow.com/questions/3716105
复制相似问题