中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android之內置和外置sdcard路徑顯示并且寫入數據的方法

發布時間:2020-10-15 05:57:22 來源:腳本之家 閱讀:253 作者:chenyu_insist 欄目:移動開發

1、效果圖片

Android之內置和外置sdcard路徑顯示并且寫入數據的方法

2、部分代碼

package com.example.sdcardcheck;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends ActionBarActivity {
 
	public static String TAG = "SdCardCheck";
	public TextView mTv;
	public Button mBtIn;
	public Button mBtout;
	public String result = "";
	public String inSdcardPath = "";
	public String outSdcaraPath = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTv = (TextView)findViewById(R.id.text);
		mBtIn = (Button)findViewById(R.id.button_in);
		mBtout = (Button)findViewById(R.id.button_out);
//		checkPerssiom();
		mBtIn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// 獲取SD卡的目錄
				saveDataToSdcard(inSdcardPath, "inSdcard_text.txt");
			}
		});
		mBtout.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				saveDataToSdcard(outSdcaraPath, "outSdcard_text.txt");
			}
		});
		checkSd();
		getCommonPath();
		getStoragePath(this, true);
		getStoragePath(this, false);
		getExternalStoreAvailableSize();
		mTv.setText(result);
	}
	
	public void checkSd() {
		String state = Environment.getExternalStorageState();
		result += "Environment.MEDIA_MOUNTED is:" + state.equals(Environment.MEDIA_MOUNTED) + "\n";
		result += "Environment.MEDIA_MOUNTED_READ_ONLY is:" + state.equals(Environment.MEDIA_MOUNTED_READ_ONLY) + "\n";
		result += "Environment.MEDIA_SHARED is:" + state.equals(Environment.MEDIA_SHARED) + "\n";
		
		Log.d(TAG, "state.equals(Environment.MEDIA_MOUNTED is:" + state.equals(Environment.MEDIA_MOUNTED));
		Log.d(TAG, "state.equals(Environment.MEDIA_MOUNTED_READ_ONLY is:" + state.equals(Environment.MEDIA_MOUNTED_READ_ONLY));
		Log.d(TAG, "state.equals(Environment.MEDIA_SHARED is:" + state.equals(Environment.MEDIA_SHARED));
	}
	
	public String getStoragePath(Context mContext, boolean is_removale) { 
		StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
		Class<?> storageVolumeClazz = null;
		try {
			storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
			Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
			Method getPath = storageVolumeClazz.getMethod("getPath");
			Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
			Object resultObject = getVolumeList.invoke(mStorageManager);
			final int length = Array.getLength(resultObject);
			for (int i = 0; i < length; i++) {
				Object storageVolumeElement = Array.get(resultObject, i);
				 String path = (String) getPath.invoke(storageVolumeElement);
				 boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
				 if (is_removale == removable) {
					if (is_removale) {
					 result += "sdcard外部存儲路經" + path + "\n";
					 outSdcaraPath = path;
					} else {
					 result += "sdcard內部存儲路經" + path + "\n";
					 inSdcardPath = path;
					}
					return path;
				 }
			}
	 } catch (ClassNotFoundException e) {
	 	e.printStackTrace();
		} catch (InvocationTargetException e) {
		 e.printStackTrace();
		} catch (NoSuchMethodException e) {
		 e.printStackTrace();
		} catch (IllegalAccessException e) {
		 e.printStackTrace();
		}
		return null;
	}
	
	
	public void showSdCard() {
		//String sdcardPath = System.getenv("EXTERNAL_STORAGE");
		String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
		result += "sdcard內部存儲路經" + sdcardPath + "\n";
		String extSdcardPaht = System.getenv("SECONDARY_STORAGE");
		result += "sdcard外部存儲路經" + extSdcardPaht + "\n";
		
	}
 /** 
  * 得到外部存儲可用的空間 
  * @return 剩余空間的大小,單位是Byte 
  */ 
 public long getExternalStoreAvailableSize(){ 
 	String state = Environment.getExternalStorageState();
  if(state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_SHARED)) { 
   // 取得sdcard文件路徑 
   File pathFile = Environment.getExternalStorageDirectory(); 
   android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath()); 
   // 獲取SDCard上每個block的SIZE 
   long nBlocSize = statfs.getBlockSize(); 
   // 獲取可供程序使用的Block的數量 
   long nAvailaBlock = statfs.getAvailableBlocks(); 
   // 計算 SDCard 剩余大小Byte 
   long nSDFreeSize = nAvailaBlock * nBlocSize ;
   if (nSDFreeSize > 1024 * 1024 * 1024) {
   	result += "外部存儲可用的空間:" + nSDFreeSize/(1024 * 1024 * 1024) + "G\n";
   } else {
   	result += "外部存儲可用的空間:" + nSDFreeSize + "Byte\n";
   }
   return nSDFreeSize; 
  } else {
  	result += "size:" + "0" + "\n";
  }
  return 0; 
 } 
 
// public void checkPerssiom() {
// 	if (Build.VERSION.SDK_INT >= 23) { 
// 		 int REQUEST_CODE_CONTACT = 101;
//  		 String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; 
//  		 //驗證是否許可權限    
//  		 for (String str : permissions) {     
//  			 if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) { 
//  				 //申請權限      
//  				 this.requestPermissions(permissions, REQUEST_CODE_CONTACT); 
//  				 return;
//  			 } else {
//  				 Log.d(TAG, "this.checkSelfPermission(str) == PackageManager.PERMISSION_GRANTED");
//  			 }
//  	 } 
// 	}
//	}
 
 public void getCommonPath() {
 	File file = Environment.getExternalStorageDirectory();
 	String path = file.getAbsolutePath();
 	result += "sdcard存儲路經" + path + "\n";
 	mTv.post(new Runnable(){
			@Override
			public void run() {
				mTv.setText(result);
			}
		});
 }
 public void saveDataToSdcard(String path, String fileName) {
 	if ("".equals(path) || path == null) {
 		result += "path is null or ''" + "\n";
 		mTv.post(new Runnable(){
				@Override
				public void run() {
					mTv.setText(result);
				}
 		});
 		return;
 	} 
 	String str = "sangfor";
		File sdDire = Environment.getExternalStorageDirectory();
		FileOutputStream outFileStream;
		try {
			outFileStream = new FileOutputStream(path + File.separator + fileName);
			outFileStream.write(str.getBytes());
			outFileStream.close();     
			Toast.makeText(MainActivity.this, fileName +"文件保存成功", Toast.LENGTH_LONG).show(); 
		} catch (Exception e) {
			e.printStackTrace();
			result += "save " + fileName +"fail" + "\n";
			mTv.post(new Runnable(){
				@Override
				public void run() {
					mTv.setText(result);
				}
 		});
			Log.e(TAG, "save file fail");
		} 
 }
}
 

以上這篇Android之內置和外置sdcard路徑顯示并且寫入數據的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

平安县| 疏附县| 穆棱市| 南皮县| 晋江市| 习水县| 斗六市| 朝阳市| 闸北区| 无锡市| 鄂托克前旗| 仙居县| 侯马市| 中西区| 黄平县| 桐乡市| 榆林市| 宁城县| 建瓯市| 固原市| 漳浦县| 当阳市| 曲水县| 布尔津县| 多伦县| 上虞市| 裕民县| 开化县| 平湖市| 宝丰县| 彭山县| 平利县| 乡城县| 隆安县| 黄平县| 登封市| 政和县| 罗城| 大姚县| 韶山市| 乌兰察布市|