2016-01-01 114 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

欢迎,如果它对你有帮助,然后接受我的答案 –

相关问题