2014-02-14 59 views
0

在我的Android應用程序,我保存的位圖文件到一個目錄在外部存儲使用下面的代碼:如果沒有任何外部存儲

String state = Environment.getExternalStorageState(); 
if (Environment.MEDIA_MOUNTED.equals(state)) 
{ 
    final String mPath = Environment.getExternalStorageDirectory().getAbsolutePath() + getResources().getString(R.string.record_folder); 

    File dir = new File(mPath); 
    if(!dir.exists()) { 
     dir.mkdirs(); 
    } 

OutputStream fout = null; 
File imageFile = new File(mPath + getResources().getString(R.string.file_name)); 

Bitmap im = Bitmap.createBitmap(b, 0, 0, canvas.getWidth(), (int) ((float)canvas.getHeight())*600/800); 

try { 
    fout = new FileOutputStream(imageFile); 
    im.compress(Bitmap.CompressFormat.JPEG, 97, fout); 
    fout.flush(); 
    fout.close(); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
} 
else 
{ 

    // ? 

} 

如果安裝了設備我的應用程序沒有外部存儲器,我該怎麼辦?

謝謝。

回答

1

然後你可以使用:

Context.getFilesDir() 

獲得路徑在內部存儲器中的文件。如果您的設備需要外部存儲器,則可以通知您的用戶並詢問是否將圖像存儲在內部存儲器中。

2

希望這可能會幫助你!

使用此代碼在內置外部存儲器中檢查介質可用性。

boolean mExternalStorageAvailable = false; 
boolean mExternalStorageWriteable = false; 
String state = Environment.getExternalStorageState(); 

if (Environment.MEDIA_MOUNTED.equals(state)) { 
    // We can read and write the media 
    mExternalStorageAvailable = mExternalStorageWriteable = true; 
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
    // We can only read the media 
    mExternalStorageAvailable = true; 
    mExternalStorageWriteable = false; 
} else { 
    // Something else is wrong. It may be one of many other states, but all we need 
    // to know is we can neither read nor write 
    mExternalStorageAvailable = mExternalStorageWriteable = false; 
} 

做你的代碼依賴於