Android,如何检索以前保存的图像并在需要时显示

时间:2013-05-15 10:32:49

标签: android android-imageview fileinputstream fileoutputstream

我使用下面的代码将摄像头意图中的图像保存到图库中的文件夹中,该文件夹效果很好。当我被问到时,我想检索要在活动中的ImageView上显示的图像,但是在尝试了各种方法/代码之后,到目前为止还没有任何工作。给定用于保存图像的路径/代码,有人可以指出我如何将其恢复正确的方向。提前谢谢 - 吉姆。

public void SaveImage(Context context,Bitmap ImageToSave, String fileName){ 
    TheThis = context; 
    String file_path = Environment.getExternalStorageDirectory()+ NameOfFolder; 
   // String CurrentDateAndTime= getCurrentDateAndTime(); 
    File dir = new File(file_path); 

    if(!dir.exists()){ 
        dir.mkdirs(); 
    } 

    File file = new File(dir, fileName + ".jpg"); 

    try { 
        FileOutputStream fOut = new FileOutputStream(file); 
        ImageToSave.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
        fOut.flush(); 
        fOut.close(); 
        MakeSureFileWasCreatedThenMakeAvabile(file); 
        AbleToSave(); 

    }  
    catch (FileNotFoundException e) {UnableToSave();} 
    catch (IOException e){UnableToSave();}        

} 

private void MakeSureFileWasCreatedThenMakeAvabile(File file) { 
    MediaScannerConnection.scanFile(TheThis, 
            new String[] { file.toString() }, null, 
            new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(String path, Uri uri) { 
            Log.e("ExternalStorage", "Scanned " + path + ":"); 
            Log.e("ExternalStorage", "-> uri=" + uri); 

        } 
    }); 

}

1 个答案:

答案 0 :(得分:1)

如果你有正确的路径和权限,这里的代码完全适合我

File rootsd = Environment.getExternalStorageDirectory();
String rootPath = rootsd.getAbsolutePath();
String pathName = rootPath + "your path here" + "image name here"; 
try {           
Drawable d = Drawable.createFromPath(pathName);
layout.setBackgroundDrawable(d);
} catch (Exception e) {
e.printStackTrace();
}

希望这有助于并享受您的工作。

相关问题