2016-01-01 118 views
-1

我正在製作一個聊天應用程序,其中用戶也可以選擇發送圖片。我想將圖像保存到應用程序文件夾中,以便我可以訪問這些圖像,以便使用圖片和文本填充聊天窗口。所以我的問題是如何將圖像添加到應用程序文件夾? 感謝將圖像保存在應用程序文件夾中?

+0

我假設你是指包應用程序可繪製文件夾。不知道你能做到這一點。您必須使用應用程序緩存存儲或內部/外部存儲文件夾,您可以在其中創建和存儲圖像 – Tasos

回答

1

首先你要創建的應用程序名稱的文件夾到SD卡,那麼你有你的文件/圖片寫進去

String SDCardRoot = Environment.getExternalStorageDirectory().toString(); 
String filename = "fileName" + ".jpg"; 
File myDir = new File(SDCardRoot + "/AppName"); 
myDir.mkdirs(); 
File file = new File(myDir, filename); 
FileOutputStream fileOutput = new FileOutputStream(file); 

//write the file into the sdcard folder specify your buffer , bufferLength 

fileOutput.write(buffer, 0, bufferLength); 
fileOutput.close(); 

那麼只有你可以從應用程序文件夾中訪問這些文件

 File imgFile; 
     String path = Environment.getExternalStorageDirectory() + "/AppName/" + ".jpg"; 
     imgFile = new File(path); 

     if (imgFile.exists()) { 
      Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
      imageView.setImageBitmap(bitmap); 
     } 
+0

非常感謝您的幫助! – revipod

+0

歡迎,如果它對你有幫助,然後接受我的答案 –

相關問題