将图像保存在应用程序文件夹中?
Saving images in app Folder?
我正在制作一个聊天应用程序,用户还可以在其中选择发送图片。我想将图像保存到应用程序文件夹中,这样我就可以访问这些图像,用以前的图片和文本消息填充聊天 window。所以我的问题是如何将图像添加到应用程序文件夹?
谢谢
首先你必须在 SD 卡中创建你的应用程序名称文件夹然后你必须将你的 file/image 写入其中
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);
}
我正在制作一个聊天应用程序,用户还可以在其中选择发送图片。我想将图像保存到应用程序文件夹中,这样我就可以访问这些图像,用以前的图片和文本消息填充聊天 window。所以我的问题是如何将图像添加到应用程序文件夹? 谢谢
首先你必须在 SD 卡中创建你的应用程序名称文件夹然后你必须将你的 file/image 写入其中
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);
}