Q:

How to access a file from another activity

I try to take pictures from camera and then I am trying to access these two files created over here in another activity with the help of fileProvider() method that I have written below, but to my bad it always shows me null at the receving point. Can some one please help me how to access the files in different activity that are created over here?
  public class FileGenerator extends Activity {
        File image1,image2,image3,image4;
        private String imagePath1,imagePath2,imagePath3,imagePath4;

        ....................
       .........................

    public void cameraShot() // This method would be called when the user clicks on a button on my activity to make a request of taking pic.
        {

                ContentValues values = new ContentValues();
                Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                //startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);

                Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                if(flipvalue == 0)
                {
                    intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, firstCapturedImageURI);
                    values.put(MediaStore.Images.Media.TITLE, "testingimage1.jpg");
                    firstCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                    startActivityForResult(intentPicture,CAMERA_DATA_FIRST);
                }

                if(flipvalue == 1)
                {
                    intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, secondCapturedImageURI);
                    values.put(MediaStore.Images.Media.TITLE, "testingimage2.jpg");
                    secondCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                    startActivityForResult(intentPicture,CAMERA_DATA_SECOND);

                }
    }

    protected void onActivityResult(int requestCode, int resultCode,Intent data)
        {
            super.onActivityResult(requestCode, resultCode, data);

            if(resultCode==RESULT_OK)
            {
                Bundle extras= data.getExtras();

                if(requestCode == 0)
                {
                    if(bitMap1 != null)
                    bitMap1.recycle();
                    bitMap1 = (Bitmap)extras.get("data");
                    imageView1.setImageBitmap(bitMap1);

                    selectedImagePath1 = getRealPathFromURI(firstCapturedImageURI);

                    imagePath1 = selectedImagePath1;
                    Log.d("Pic Path>>>", selectedImagePath1);

                    image1 = new File(selectedImagePath1);

                    flipvalue =1;

                }
                           if (requestCode == 1)
                {
                    if(bitMap2 != null)
                    bitMap2.recycle();
                    bitMap2 = (Bitmap)extras.get("data");
                    imageView2.setImageBitmap(bitMap2);

                    selectedImagePath2 = getRealPathFromURI(secondCapturedImageURI);

                    imagePath2 = selectedImagePath2;
                    Log.d("Pic Path>>>", selectedImagePath2);
                    image2 = new File(selectedImagePath2);

                    flipvalue =2;

                }
    }

protected File fileProvider(int i)
    {
        if(i==1)
        return image1;
        else
        return image2;

    }

public String getRealPathFromURI(Uri contentUri)
    {
        try
        {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e)
        {
            return contentUri.getPath();
        }
    }
}

Below is the activity where I try to receive these files.
public class FileReceiver extends Activity{
File receivedimage1, receivedimage2;
FileGenerator filegen = new FileGenerator();
               ..........................
                   ............................

           receivedimage1 = filegen.fileProvider(1);
            Log.d("File 1 ","file1 object"+receivedimage1);// always shows null

            receivedimage2 = filegen.fileProvider(2);
             Log.d("File 2 ","file2 object"+receivedimage2);// shows null

}

P.S. -- Before trying this, I used to take the strings(i.e., selectedImagePath1, selectedImagePath2) to another activity, then used these strings to create files. But don't know why this somehow corrupts my image files that I create. So, I followed the above code way, still no luck.

A:

you will need to use Intent for sending file paths to other Activities and if you have more then two Activity then use SharedPreferences for Stroring files path and retrive paths in other Activities from SharedPreferences. you can store paths in SharedPreferences as :
 SharedPreferences pathPrefs = FileGenerator.this.getSharedPreferences(
                                             "filepathPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = pathPrefs.edit();
        prefsEditor.putString("image1",image1.getAbsolutePath().toString());
        prefsEditor.putString("image2",image2.getAbsolutePath().toString());
        prefsEditor.commit();

and in other Activities get file paths from SharedPreferences as:
SharedPreferences pathPrefs = 
                   this.getSharedPreferences("filepathPrefs", MODE_WORLD_READABLE);
String image1 = pathPrefs.getString("image1", "defaultpath");
String image2 = pathPrefs.getString("image2", "defaultpath");
// now use both file path in your code..

