Android 将图像存储和检索到SD卡中
我有一组图像URL。我把它下载到位图。现在我想将这些图像存储到SD卡/项目文件夹中。如果我没有这样的文件,我必须创建它。我现在所做的是:Android 将图像存储和检索到SD卡中,android,android-sdcard,Android,Android Sdcard,我有一组图像URL。我把它下载到位图。现在我想将这些图像存储到SD卡/项目文件夹中。如果我没有这样的文件,我必须创建它。我现在所做的是: String path = Environment.getExternalStorageDirectory().toString(); OutputStream fOut = null; File file = new File(path, imageName); if(!file.exists()) { file.mkdir(); try {
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, imageName);
if(!file.exists()) {
file.mkdir();
try {
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(), "file://"
+ file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
但我不会将图像插入SD卡。我的代码有什么问题?请回复。提前感谢。尝试使用以下代码:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
String fileName = edtNameImage.getText().toString().trim();//this can be changed
if (fileName.equalsIgnoreCase("")) {
Toast.makeText(context, "Fields cannot be left blank",
Toast.LENGTH_SHORT).show();
return false;
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + fileName);
// write the bytes in file
FileOutputStream fo;
try {
file.createNewFile();
fo = new FileOutputStream(file);//snapshot image is the image to be stored.
if (snapShotImage!=null)
{
snapShotImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
}else{
return false;
}
fo.write(bytes.toByteArray());
// ChartConstants.IMAGE_STORAGE++;
fo.flush();
fo.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return true;
您还可以使用一些额外的代码行检查名称中的重复性。
如果有帮助,请告诉我。尝试使用以下代码:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
String fileName = edtNameImage.getText().toString().trim();//this can be changed
if (fileName.equalsIgnoreCase("")) {
Toast.makeText(context, "Fields cannot be left blank",
Toast.LENGTH_SHORT).show();
return false;
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + fileName);
// write the bytes in file
FileOutputStream fo;
try {
file.createNewFile();
fo = new FileOutputStream(file);//snapshot image is the image to be stored.
if (snapShotImage!=null)
{
snapShotImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
}else{
return false;
}
fo.write(bytes.toByteArray());
// ChartConstants.IMAGE_STORAGE++;
fo.flush();
fo.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return true;
您还可以使用一些额外的代码行检查名称中的重复性。
如果有帮助,请告诉我
}catch(filenotfound异常){
Log.v(this.toString(),“块中捕获异常”);
}
斯利拉姆
}catch(filenotfound异常){
Log.v(this.toString(),“块中捕获异常”);
}
Sriram与代码有关的问题:
1) 您正在使用文件名创建目录。改为只使用“路径”尝试mkdir() 2) 仅将不带“file://”的“file.getAbsolutePath()”传递给insertImage()函数
3) 有一个替代api,您无需在本地为其创建更多文件。将位图直接传递给该对象
希望这有帮助。代码问题:
1) 您正在使用文件名创建目录。改为只使用“路径”尝试mkdir() 2) 仅将不带“file://”的“file.getAbsolutePath()”传递给insertImage()函数
3) 有一个替代api,您无需在本地为其创建更多文件。将位图直接传递给该对象 希望这有帮助