Q: How to set image from text file in android I want to set image to ImageView in following way
1) when click on button image gallery open
2) select any image and set to image view & store that image path to text file
i did all this work but when application close, ones start again, the ImageView does not shows the previous image via retrieve image path from text file
code is below
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    img = (ImageView) findViewById(R.id.imageView1);
    ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT); 
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
        }

    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
        if (requestCode == SELECT_PICTURE) { 
            Uri selectedImageUri = data.getData(); 
            selectedImagePath = getPath(selectedImageUri); 
            System.out.println("Image Path : " + selectedImagePath); 
            img.setImageURI(selectedImageUri); 
            File myFile = new File("/sdcard/ImageLocation.txt"); 
            try { 
                myFile.createNewFile(); 
                FileOutputStream fOut = new FileOutputStream(myFile); 
                OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
                myOutWriter.append(selectedImagePath); 
                myOutWriter.close(); 
                fOut.close(); 
                Toast.makeText(getBaseContext(),"Done writing SD ",Toast.LENGTH_SHORT).show();
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace();
            } 
        }
    }
}

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 

    return cursor.getString(column_index); 
}


A: Where are you reading the ImagePath on restart? Please show the code for the same. Also, while writing the ImagePath to the text file you should do the following
File myFile = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt"); 
    try { 
        if(!myFile.exists()) {
            myFile.createNewFile();
        }
        FileOutputStream fOut = new FileOutputStream(myFile); 
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
        myOutWriter.append(selectedImagePath); 
        myOutWriter.close(); 
        fOut.close(); 
    } catch (IOException e) { 
        e.printStackTrace();
    }

While reading the file also you should read using Environment.getExternalStorageDirectory()
EDIT:
If your intention is to just look into the Image Gallery then you should not use intent.setAction(Intent.ACTION_GET_CONTENT);, change it to intent.setAction(Intent.ACTION_PICK);.
Now, the below will be your file path reading function (your file writing code will always overwrite the existing stored path..so you will always have only one path stored....if you desire to store more than one image path then you should modify both your file writing and the below file reading part).
public String getStoredPath() {
    String path = null;
    File myFile = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt"); 
    if(myFile.exists()) {
        //Read text from file
        try { 
            BufferedReader br = new BufferedReader(new FileReader(myFile)); 
            String line; 
            // Below while loop will only run once as your file writing 
            // always overwrites the existing line
            while ((line = br.readLine()) != null) { 
                path = line;
            } 
        } catch (IOException e) { 

        } 
    }

    return path;
} 

Then at the place where you want to set the image uri. 
String imagePath = getStoredPath();
if(imagePath != null) {
    img.setImageURI(Uri.parse(imagePath));
}

Be sure to make the suggested change for file write. Also, if it is only one image path you want to store and fetch, then consider using Shared Preferences.
